https://github.com/perry-mitchell/pfa
Crazy-simple Partial Function Application library
https://github.com/perry-mitchell/pfa
currying partial-functions
Last synced: about 20 hours ago
JSON representation
Crazy-simple Partial Function Application library
- Host: GitHub
- URL: https://github.com/perry-mitchell/pfa
- Owner: perry-mitchell
- License: mit
- Created: 2017-05-22T10:03:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-21T17:11:35.000Z (over 7 years ago)
- Last Synced: 2025-09-04T03:32:34.844Z (about 1 month ago)
- Topics: currying, partial-functions
- Language: JavaScript
- Size: 54.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pfa
Crazy-simple Partial Function Application library[](https://travis-ci.org/perry-mitchell/pfa) [](https://www.npmjs.com/package/pfa)
## 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](https://www.npmjs.com/package/clone) takes 3 parameters: `clone(obj, isCircular, depth)` - imagine that we always use the same values for the latter 2 arguments:
```javascript
const result = clone(myObj, false, 50);
```It'd be much nicer to just call `clone(myObj)`, so we could in turn do something like the following:
```javascript
const { partialApply, _ } = require("pfa");
const _clone = require("clone");const clone = partialApply(
_clone, // The function to partially apply arguments
_, // An argument we will provide later
false, // An argument to always provide at the second position
50 // An argument to always provide at the third position
);const myObj = { key: "value" };
const clonedObj = clone(myObj); // equiv: clone(myObj, false, 50);
```## Usage
**pfa** exports 3 items:* `partialApply`: The partial application function
* `_`: Placeholder for unknown arguments
* `partialApplyRight`: Partial application to the right side of a functionIt also exports the `partialApply` function as the default, so CommonJS and ES6 imports can both be used neatly:
```javascript
const { partialApply, _ } = require("pfa");
```_Or:_
```javascript
import partialApply, { _ } from "pfa";
```You can also apply to the right-hand side of a function using `partialApplyRight`, like so:
```javascript
import { partialApplyRight } from "pfa";function myMethod(target, defaultValue) {
// some functionality
}const shorthandMethod = partialApplyRight(myMethod, {});
shorthandMethod({ some: "argument" }); // 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](https://github.com/perry-mitchell/pfa/blob/master/API.md) for more information.
## Installation
Simply install as a dependency:```shell
npm install pfa --save
```