xxxxxxxxxx
//We have the following array:
let arr1 = ["it's Sunny in", "", "London"];
//Map creates multidimensional array:
arr1.map(item => item.split(" "));
//returns
(3) [Array(3), Array(1), Array(1)]
0: (3) ["it's", "Sunny", "in"]
1: [""]
2: ["London"]
// visually: [["it's", "Sunny", "in"], [""], ["London"]]
//While flatmap makes an array flattened to dimension 1 (“unidimensional” so to say):
arr1.flatMap(item => item.split(" "));
//returns
(5) ["it's", "Sunny", "in", "", "London"]
0: "it's"
1: "Sunny"
2: "in"
3: ""
4: "London"
//visually: ["it's", "Sunny", "in", "", "London"]
xxxxxxxxxx
val maybeNumber: Option[Int] = Some(42)
val doubled: Option[Int] = maybeNumber.map(n => n * 2)
println(doubled) // Output: Some(84)