Skip to content

Instantly share code, notes, and snippets.

@m3g4p0p
Last active November 17, 2019 19:13
Show Gist options
  • Save m3g4p0p/7ae67a5b0cfa0e3d64ec37e51af6211a to your computer and use it in GitHub Desktop.
Save m3g4p0p/7ae67a5b0cfa0e3d64ec37e51af6211a to your computer and use it in GitHub Desktop.
{
"name": "transduce",
"version": "0.1.0",
"main": "transduce.js",
"license": "MIT",
"author": {
"name": "m3g4p0p"
}
}
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