Skip to content

Instantly share code, notes, and snippets.

@thomas-jeepe
Last active December 12, 2015 18:08
Show Gist options
  • Save thomas-jeepe/cdf26fd93cd7ae440046 to your computer and use it in GitHub Desktop.
Save thomas-jeepe/cdf26fd93cd7ae440046 to your computer and use it in GitHub Desktop.
some useful funcs
let pipe = (...funcs) => (...args) => funcs.reduce((vals, func) => (
vals === args ? func(...vals) : func(vals)
), args)
let curry = (func) => function newCurry (...args) {
return args.length < func.length ? newCurry.bind(null, ...args) : func.bind(null, ...args)()
}()
let map = curry((func, arr) => {
let l = arr.length
let newArr = []
while(l--) {
newArr[l] = func(arr[l])
}
return newArr
})
let reduce = curry((func, arr, init = null) => {
let acc = init
for(let i = 0, x; x = arr[i]; ++i) {
acc = func(acc, arr[i], i, arr)
}
return acc
})
let flatten = (arr) => reduce((acc, val) => (
acc.concat(Array.isArray(val) ? flatten(val) : val)
), arr, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment