Last active
December 12, 2015 18:08
-
-
Save thomas-jeepe/cdf26fd93cd7ae440046 to your computer and use it in GitHub Desktop.
some useful funcs
This file contains 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
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