Reputation: 786
I have a list of integers:
val mylist = List(1, 2, 3, 4)
What I want to do is to map the element which are even numbers in mylist, and multiply them by 2.
Maybe the code should be:
mylist.map{ case x%2==2 => x*2 }
I expect the result to be List(4, 8) but it's not. What is the correct code?
I know I could realize this function by using filter + map
a.filter(_%2 == 0).map(_*2)
but is there some way to realize this function by only using map()?
Upvotes: 0
Views: 679
Reputation: 8851
map
does not reduce number of elements in transformation.
filter + map
is right approach.
But if single method is needed, use collect
:
mylist.collect{ case x if x % 2 == 0 => 2 * x }
Edit:
withFilter + map
is more efficient than filter + map
(as withFilter
does not create intermediate collection, i.e. it works lazily):
mylist.withFilter(_ % 2 == 0).map(_ * 2)
which is same as for
:
for { e <- mylist if (e % 2 == 0) } yield 2 * e
Upvotes: 3