<div id="root"></div>
// this is just here to display results to DOM
const addResultToDOM = (...args) => $('#root').append(`<div>${args.join(' ')}</div>`)
const compose = (...fns) => arg => fns.reduce((acc, fn) => fn(acc), arg)
const curry = (uncurriedFn, ...args) => {
// you can get the arity of a function by calling length on the fn
const arity = uncurriedFn.length
const accumulator = (...accArgs) => {
let argsCopy = [...args] //
if (accArgs.length > 0) {
argsCopy = [...argsCopy, ...accArgs]
}
if (args.length >= arity) return uncurriedFn.apply(null, argsCopy)
return curry.apply(null, [uncurriedFn, ...argsCopy]) // recurse
}
// if any args passed in, pass them to the accumulator
return args.length >= arity ? accumulator() : accumulator
}
const nums = [1, 2, 3, 4, 5]
const evenNums = nums.filter(num => num % 2 === 0)
evenNums.map(i => addResultToDOM(i)) // Just to show
This Pen doesn't use any external CSS resources.