scala 如何在Scala中结合过滤器和地图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32234132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to combine filter and map in scala?
提问by eddard.stark
I have List[Int]in Scala. The List is List(1,2,3,4,5,6,7,8,9,10). I want to filterthe list so that it only has even numbers. And I want to muliply the numbers with 2. Is it possible?
我List[Int]在Scala. 名单是List(1,2,3,4,5,6,7,8,9,10)。我想要filter列表,以便它只有偶数。我想将数字乘以 2。这可能吗?
I hope I have explained the question clearly. If u have any questions then please ask. THanks in advance.
我希望我已经清楚地解释了这个问题。如果您有任何问题,请提问。提前致谢。
回答by cmbaxter
As I state in my comment, collectshould do what you want:
正如我在评论中所述,collect应该做你想做的:
list.collect{
case x if x % 2 == 0 => x*2
}
The collectmethod allows you to both specify a criteria on the matching elements (filter) and modify the values that match (map)
该collect方法允许您在匹配元素 ( filter)上指定条件并修改匹配 ( map)
And as @TravisBrown suggested, you can use flatMapas well, especially in situations where the condition is more complex and not suitable as a guard condition. Something like this for your example:
正如@TravisBrown 建议的那样,您也可以使用flatMap,尤其是在条件更复杂且不适合作为保护条件的情况下。你的例子是这样的:
list.flatMap{
case x if x % 2 == 0 => Some(x*2)
case x => None
}
回答by elm
A for comprehension (which internally unfolds into a combination of mapand withFilter) as follows,
一种用于理解(其内部展开成的组合map和withFilter)如下,
for (x <- xs if x % 2 == 0) yield x*2
Namely
即
xs.withFilter(x => x % 2 == 0).map(x => x*2)
回答by Cacho Santa
This should do the work for you:
这应该为您完成工作:
Filter first when the condition is p % 2 == 0(for getting only even numbers).
当条件为p % 2 == 0(仅获取偶数)时首先过滤。
And then use mapto multiply those even numbers by 2.
然后用map这些偶数乘以 2。
var myList = List(1,2,3,4,5,6,7,8,9,10).filter(p => p % 2 == 0).map(p => {p*2})
回答by om-nom-nom
As @cmbaxter said, collect suits your need perfectly. The other nice thing about collect is that it figures out resulting type in case you're filtering by class:
正如@cmbaxter 所说,collect 非常适合您的需求。collect 的另一个好处是它可以计算出结果类型,以防您按类进行过滤:
scala> trait X
// defined trait X
scala> class Foo extends X
// defined class Foo
scala> class Bar extends X
// defined class Bar
scala> val xs = List(new Foo, new Bar, new Foo, new Bar)
// xs: List[X] = List(Foo@4cfa8227, Bar@78226c36, Foo@3f685162, Bar@11f406f8)
scala> xs.collect { case x: Foo => x }
// res1: List[Foo] = List(Foo@4cfa8227, Foo@3f685162)
On par, filter can't be that smart (see List[Foo]vs List[X]):
同样,过滤器不能那么聪明(参见List[Foo]vs List[X]):
scala> xs.filter { case x: Foo => true; case _ => false }
// res3: List[X] = List(Foo@4cfa8227, Foo@3f685162)
回答by Dean Sha
I tend to like the filter approach.
我倾向于喜欢过滤器方法。
val list1 = List(1,2,3,4,5,6,7,8,9,10)
list1.filter(x => x%2 == 0).map(_*2)
回答by F. P. Freely
How about a good old fashioned fold?
一个好的老式折叠怎么样?
xs.foldLeft(List[Y]()) { (ys, x) =>
val z = calculateSomethingOnX(x)
if (someConditionOnZ(z))
Y(x, z) :: ys
else
ys
}

