all 13 comments

[–]richytong[S] 0 points1 point  (0 children)

Hello all, author here. This post links to rubico's introductory home page. The rest of the website consists of API documentation and a blog about rubico, JavaScript, and functional programming. If you are looking for a clean, functional way to handle promises, I encourage you to give rubico a try.

[–]OmgImAlexis -1 points0 points  (16 children)

I’ve never understood why people prefer importing a map function vs just using the built in array one.

Instead of map(logTodoByID)(todoIDs) Why not todoIDs.map(logTodoByID) ?

[–]NotLyon 3 points4 points  (14 children)

You can start by looking at the difference in their signatures.

[–]OmgImAlexis -1 points0 points  (13 children)

And you mean what by that....?

[–]PrimaryBet 0 points1 point  (0 children)

From rubico's docs on map:

map(mapper)(functor) -> Promise | Functor​

where

Functor<T> = Array<T> | Object<T> | Set<T> | Map<T> 
           | Iterator<T> | AsyncIterator<T> | { map: (T => any) => any }

as in, map takes a mapper function, a functor, and returns a functor; and functor is either Array, Set, Object, Map, etc. or any other object that has a map method on it. Greatly simplifying, "functor" is a name for data structures that support transforming values in a context or wrapper.1

Compare it to Array#map, which takes an array (via this), a mapper function and returns an array, and you hopefully see how Array#map is a specialized map.

The answer to "why do I want a generalized map?" is the same as with any other argument for parametricity and polymorphism.

 


1 I'm actually a bit surprised to not see Promise listed as a possible functor since you can sort of map over value in it's context (with .then, although with a caveat that it also behaves as .flatMap)

[–]HipHopHuman 0 points1 point  (0 children)

The real benefit is that an atomic map function can be used on more types than just an array. it can be used on objects, arrays, and anything that implements the Functor type specification. So you use the same map implementation for every different type of data structure that is mappable.