-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpartialApply.js
34 lines (31 loc) · 1.06 KB
/
partialApply.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
import curry from './curry';
import _enforceFunction from './internal/_enforceFunction';
import { Iterable } from 'immutable';
function partialApply(operation, args) {
_enforceFunction(operation);
const isArray = Array.isArray(args);
if (!isArray && !Iterable.isOrdered(args)) {
throw new Error(
`expected \`args\` to be an Array or OrderedIterable but got \`${args}\``
);
}
const arrayArgs = isArray ? args : args.toArray();
return operation.bind(null, ...arrayArgs);
}
/**
* Like `transmute/partial`, but takes an Array or Iterable of arguments to pass
* to `operation` rather than a dynamic number of args. Unlike `partial` it is
* curried.
*
* partial : partialApply :: Function.prototype.call : Function.prototype.apply
*
* @example
* const add = (a, b, c) => a + b + c;
* const add11 = partialApply(add, [5, 6]);
* add11(7); // returns 18
*
* @param {Function} operation the function to bind.
* @param {Array|Iterable} args ordered collection of arguments to bind to `fn`.
* @return {Function}
*/
export default curry(partialApply);