-
Notifications
You must be signed in to change notification settings - Fork 15
/
curry.js
74 lines (71 loc) · 1.96 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const curryArity = require('./_internal/curryArity')
/**
* @name curry
*
* @synopsis
* ```coffeescript [specscript]
* type __ = Symbol(placeholder)
* type ArgsWithPlaceholder = Array<__|any>
*
* args ArgsWithPlaceholder
* moreArgs ArgsWithPlaceholder
*
* curry(
* func function,
* ...args
* ) -> curriedFuncOrResult function|any
*
* curriedFuncOrResult(...moreArgs) -> anotherCurriedFuncOrResult function|any
* ```
*
* @description
* Enable partial application of a function's arguments in any order. Provide the placeholder value `__` to specify an argument to be resolved in the partially applied function.
*
* ```javascript [playground]
* const add = (a, b, c) => a + b + c
*
* console.log(curry(add, 'a', 'b', 'c')) // 'abc'
* console.log(curry(add)('a', 'b', 'c')) // 'abc'
* console.log(curry(add, 'a')('b', 'c')) // 'abc'
* console.log(curry(add, 'a', 'b')('c')) // 'abc'
* console.log(curry(add)('a')('b')('c')) // 'abc'
*
* console.log(curry(add, __, 'b', 'c')('a')) // abc
* console.log(curry(add, __, __, 'c')('a', 'b')) // abc
* console.log(curry(add, __, __, 'c')(__, 'b')('a')) // abc
* ```
*/
const curry = (func, ...args) => curryArity(func.length, func, args)
/**
* @name curry.arity
*
* @synopsis
* ```coffeescript [specscript]
* type __ = Symbol(placeholder)
* type ArgsWithPlaceholder = Array<__|any>
*
* args ArgsWithPlaceholder
* moreArgs ArgsWithPlaceholder
*
* curry.arity(
* arity number,
* func function,
* ...args
* ) -> curriedFuncOrResult function|any
*
* curriedFuncOrResult(...moreArgs) -> anotherCurriedFuncOrResult function|any
* ```
*
* @description
* `curry` with specified arity (number of arguments taken by the function) as the first parameter.
*
* ```javascript [playground]
* const add = (a, b, c = 0) => a + b + c
*
* console.log(curry.arity(2, add, 1, 2)) // 3
* ```
*/
curry.arity = function curryArity_(arity, func, ...args) {
return curryArity(arity, func, args)
}
module.exports = curry