Last active
November 17, 2019 19:13
-
-
Save m3g4p0p/7ae67a5b0cfa0e3d64ec37e51af6211a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "transduce", | |
"version": "0.1.0", | |
"main": "transduce.js", | |
"license": "MIT", | |
"author": { | |
"name": "m3g4p0p" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const pipe = (...fns) => value => fns.reduce((result, current) => current(result), value) | |
export const curry = fn => function accumulate () { | |
return arguments.length < fn.length | |
? accumulate.bind(this, ...arguments) | |
: fn.apply(this, arguments) | |
} | |
export const append = (array, ...values) => array.concat(values) | |
export const appendUnique = (array, ...values) => | |
array.concat( | |
values.filter(value => | |
array.indexOf(value) === -1 | |
) | |
) | |
export const makeFilterReducer = curry( | |
(predicate, combine) => | |
(result, current) => | |
predicate(current) | |
? combine(result, current) | |
: result | |
) | |
export const makeMapReducer = curry( | |
(mapper, combine) => | |
(result, current) => | |
combine(result, mapper(current)) | |
) | |
export const transduce = curry( | |
(transducer, transformer, initial, values) => | |
values.reduce(transducer(transformer), initial) | |
) | |
export const makeTapTransducer = curry( | |
(callback, combine) => | |
(...args) => { | |
callback(null, ...args) | |
return combine(...args) | |
} | |
) | |
export const deepFlatTransducer = combine => | |
(result, current) => | |
current != null && current[Symbol.iterator] | |
? transduce(deepFlatTransducer, combine, result)(current) | |
: combine(result, current) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment