Fromable
Fromable allows you take any iterable, tranform it using .map
and .filter
, and then output it into a collection, without creating intermediate values.
Motivation
arr.filter(isSomething).map(transform);
will allocate two new arrays into memory, when you only want one. Each time you run .filter
or .map
, you're creating a whole new array, which will need to be garbage collected.
Additionally, it only operates on arrays. filtering/mapping on Sets/Maps/strings is not yet possible.
Install
npm i fromable
Usage
from
will accept any Iterable (array, string, Set, Map, etc).
into
will accept a number, string, array, Set, Map, or object. It accepts an optional second argument, which will handle custom combinations.
; // 😆 // chain map and filter on arraysfrom .mapval + 2 .filterval % 2 === 0 .mapval ** 2 .into //=> [16, 36, 64] // accumulate into a numberfromnew Set .mapval + 2 .filterval > 4 .into0 //=> 18 // add entries to an objectfrom"abc" .map .into //=> { a: 1, b: 2, c: 3} // zip together multiple iterablesfrom, , .mapfirst + second + third .into //=> [12, 15, 18] // use custom combinerfrom .intonew Queue,queue.enqueuecurr //=> Queue <1, 2, 3, 4>