-
Notifications
You must be signed in to change notification settings - Fork 1
/
curryWithPlaceholder.js
54 lines (48 loc) · 1.15 KB
/
curryWithPlaceholder.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
function curry(fn) {
// your code here
return function curried(...args) {
const complete =
args.length >= fn.length &&
!args.slice(0, fn.length).includes(curry.placeholder);
if (complete) {
return fn.apply(this, [...args]);
}
return function (...args2) {
const mergedArgs = mergeArguments(args, args2);
return curried.apply(this, [...mergedArgs]);
};
};
}
function mergeArguments(args, args2) {
let i = 0;
let j = 0;
let finalArgs = [];
while (i < args.length && j < args2.length) {
if (args[i] === curry.placeholder) {
finalArgs.push(args2[j]);
i++;
j++;
} else {
finalArgs.push(args[i]);
i++;
}
}
while (i < args.length) {
finalArgs.push(args[i]);
i++;
}
while (j < args2.length) {
finalArgs.push(args2[j]);
j++;
}
return finalArgs;
}
curry.placeholder = Symbol();
const join = (a, b, c) => {
return `${a}_${b}_${c}`;
};
const curriedJoin = curry(join);
const _ = curry.placeholder;
console.log(curriedJoin(1, 2, 3)); // '1_2_3'
console.log(curriedJoin(_, 2)(1, 3)); // '1_2_3'
console.log(curriedJoin(_, _, _)(1)(_, 3)(2)); // '1_2_3'