pfa
Crazy-simple Partial Function Application library
About
Partial function application is way cool! Imagine having a function that you use in multiple places, but its parameters are often repeated due to the configuration not changing. You could wrap the function yourself, or you could use a library like pfa to partially apply arguments to the function.
For instance, the npm library clone takes 3 parameters: clone(obj, isCircular, depth)
- imagine that we always use the same values for the latter 2 arguments:
const result = ;
It'd be much nicer to just call clone(myObj)
, so we could in turn do something like the following:
const partialApply _ = ;const _clone = ; const clone = ; const myObj = key: "value" ;const clonedObj = ; // equiv: clone(myObj, false, 50);
Usage
pfa exports 3 items:
partialApply
: The partial application function_
: Placeholder for unknown argumentspartialApplyRight
: Partial application to the right side of a function
It also exports the partialApply
function as the default, so CommonJS and ES6 imports can both be used neatly:
const partialApply _ = ;
Or:
;
You can also apply to the right-hand side of a function using partialApplyRight
, like so:
; { // some functionality} const shorthandMethod = ;; // calls myMethod with 2 arguments
pfa supports NodeJS 6.10 onwards. For browser usage you should transpile to ES5 using something like BabelJS.
Consult the API documentation for more information.
Installation
Simply install as a dependency:
npm install pfa --save