Function: partialApply()
Binds the first argument to a function, returning a new function that takes the remaining arguments.
partialApply is curried. See curry.
Example
const concat = (a: string, b: string) => a + b;
const toUsername = partialApply("@", concat);
toUsername("elias"); // => "@elias"
Manual currying
Due to limitations of TypeScript, longlast's curry function does not
support currying functions with type parameters. You can, however, use
partialApply to create manually curried functions with type parameters, as
in the following example:
// Two-argument signature
function filter<T>(
callback: (elem: T) => boolean,
array: T[],
): T[];
// One-argument signature
function filter<T>(
callback: (elem: T) => boolean,
_?: never,
): (array: T[]) => T[];
// Implementation
function filter(callback: any, array?: any): any {
if (arguments.length === 1) {
return partialApply(callback, filter);
}
return (array as any).filter(callback);
}
Note the second parameter in the "one-argument signature," _?: never. Its
purpose is to prevent the function being called with a second argument of
the wrong type.
A downside of this approach is that the implementation of the function is not typesafe, and you must write each call signature yourself.
Call Signature
partialApply<
Arg,Rest,Return>(arg:Arg,f: (a:Arg, ...rest:Rest) =>Return):PartiallyApplied<Rest,Return>
Defined in: pkg/partial-apply/src/index.ts:20
Type Parameters
| Type Parameter |
|---|
Arg |
Rest extends any[] |
Return |
Parameters
| Parameter | Type |
|---|---|
arg |
Arg |
f |
(a: Arg, ...rest: Rest) => Return |
Returns
PartiallyApplied<Rest, Return>
Call Signature
partialApply<
Arg>(arg:Arg,f?:undefined): <Rest,Return>(f: (arg:Arg, ...rest:Rest) =>Return) =>PartiallyApplied<Rest,Return>
Defined in: pkg/partial-apply/src/index.ts:25
Type Parameters
| Type Parameter |
|---|
Arg |
Parameters
| Parameter | Type |
|---|---|
arg |
Arg |
f? |
undefined |
Returns
<
Rest,Return>(f: (arg:Arg, ...rest:Rest) =>Return):PartiallyApplied<Rest,Return>
Type Parameters
| Type Parameter |
|---|
Rest extends any[] |
Return |
Parameters
| Parameter | Type |
|---|---|
f |
(arg: Arg, ...rest: Rest) => Return |
Returns
PartiallyApplied<Rest, Return>