-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurry.js
36 lines (28 loc) · 851 Bytes
/
curry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// curry from crocks. copied here as es module
/** @license ISC License (c) copyright 2016 crocks original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
// isFunction : a -> Boolean
function isFunction(fn) {
return typeof fn === 'function'
}
function applyCurry(fn, arg) {
if(!isFunction(fn)) { return fn }
return fn.length > 1 ? fn.bind(null, arg) : fn.call(null, arg)
}
// curry : ((a, b, c) -> d) -> a -> b -> c -> d
export function curry(fn) {
return function(...xs) {
const args =
xs.length ? xs : [ undefined ]
if(args.length < fn.length) {
return curry(Function.bind.apply(fn, [ null ].concat(args)))
}
const val = args.length === fn.length
? fn.apply(null, args)
: args.reduce(applyCurry, fn)
if(isFunction(val)) {
return curry(val)
}
return val
}
}