7 ms·
I was pretty puzzled by the method name "map" in the examples. Why would you call it "map" when there are no arrays or lists involved?
by zestyping 5y ago
I was pretty puzzled by the method name "map" in the examples. Why would you call it "map" when there are no arrays or lists involved?
- RyEgswuCsn 5y agoBecause of the mathematical "map" [0] perhaps? [0]: https://en.wikipedia.org/wiki/Map_(mathematics) https://en.wikipedia.org/wiki/Map_(mathematics)
- cheriot 5y agoAre you thinking about Maybe.map in particular? It's the same name because it does the same thing: run this function on the value inside if there is one. It's a Functor :)
- dragonwriter 5y agoA Maybe is effectively isomorphic to an immutable list whose cardinality can only be zero or one.
- CodeAndCuffs 5y agox.map(f) doesnt mean "apply function f to each element in this list that we called x" x.map(f) means "apply the function f to the value within x, where x is a container of some sort" [].map(f) would do nothing, because there is nothing to pull out. ['adam', 'bill'].map(f) takes 'adam' out of the array, applies f to it, and puts it back in the array, then does the same with 'bill'. Just(5).map(f) takes 5 out of the Maybe, applies f to it, and puts it back in nothing.map(f) returns nothing. So we can map functions to values in Arrays, map functions to values in Maybes and even map functions to values in Eithers. Each time the function doesnt care that the value is in a Maybe or an Array or an Either.
- bobbylarrybobby 5y agoThe only requirement for something to be mappable is that it be iterable
- growlfrench 5y agoBrian Lonsdorf has a fun course where he explains these things: https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript https://egghead.io/courses/professor-frisby-introduces-compo...
- r-zip 5y agoBecause the Result and Maybe types in the examples are functors: https://en.wikipedia.org/wiki/Functor_(functional_programming) https://en.wikipedia.org/wiki/Functor_(functional_programmin...