Scala map and flatMap the missing explanation
Map
- you provide a function to map
item -> item - map scans every item in the input container
- map applies your function to each item
- map creates the same container as was originally provided (
ListforList) and wraps the results in it.
flatMap
- you provide a function to map
item -> Container[Item]soflatMapexpects you to create a container for each item you scan - flatMap then creates the same external container
- flatMap then strips each item from the container it’s in and adds it to the external container which it’s creating the same - as the original container provided.