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 (
List
forList
) and wraps the results in it.
flatMap
- you provide a function to map
item -> Container[Item]
soflatMap
expects 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.