#SCALA #NOTES Difference between list.foreach() and list.map()

[scala]val l = List(1,2,3,4,5)
val lAfterForEach = l.foreach(x => x * 10)
val lAfterMap = l.map(x => x * 10)
println(“Original List: ” + l)
println(“For each operates on each element, returns nothing:” + lAfterForEach)
println(“Map is like foreach but returns new post-processed list: ” + lAfterMap)[/scala]

output:
[bash]Original List: List(1, 2, 3, 4, 5)
For each operates on each element, returns nothing:()
Map is like foreach but returns new post-processed list: List(10, 20, 30, 40, 50)[/bash]

Leave a Reply

Your email address will not be published. Required fields are marked *