Q:
Why is the max in `map` less than the max in `flatMap`?
Why does the following code return a list of length 8 while the one in flatMap returns a list of length 3?
val xs = List(1, 2, 3, 4, 5, 6, 7, 8)
val ys = xs.map(_ + 1)
println(xs.length)
println(ys.length)
xs.max + 1
A:
Well, this is because map() applies a function to the elements of a list and returns a list of the values it got from applying the function to each element.
That is why it will be shorter.
On the other hand, flatMap() applies a function to each element of a list and returns a list of the function outputs.
So it will be longer because each element of the list will have a different length for the first element of the list.
The Daily Show with Jon Stewart To Air on Hulu - scottily
======
jvandemar
I didn't see this. Why was it so hard to find?
~~~
coderdude
After a cursory Google search, I didn't see anything.
Case: 14-41291 Document: 00513151391 Page: 1 Date Filed: 08/12/2015
IN THE UNITED STATES COURT OF APPEALS
FOR THE FIFTH CIRCUIT
No. 14-41291
Conference Calendar ac619d1d87
Related links:
Comments