Symbiotic, high-performance currying and partial application
MIT License
Symbiotic, performent currying and partial application.
Server | Client |
---|---|
npm install --save purry |
bower install --save purry or component install jasonkuhrt/purry
|
Purry greatly facilitates use of and interopability between two helpful techniques that come from the world of functional programming: Currying, and Partial Application. If these concepts are not familiar to you go read their background. Purry docs assume fluency in the topics.
The act of supplying fewer arguments to a function f1
than it is has parameters which (given f1
is curried) returns a new function f2
whose parameters are those that were not argued against f1
before. For example:
// Imagine f1 is this add function:
add(1, 2)
// 3
// f2 would be derived like so:
var add1 = add(1)
// Ready to be used in high-order contexts, etc.:
[1,2,3,4,5].map(add1)
// [2,3,4,5,6]
Canocially, currying works left-to-right.
Learn more in @fogus's tour de force Functional JavaScript in the "Currying" section in Chapter 5. Function-Building Functions. It includes many examples, use-cases, diagrams, etc.
In lieu of buying the book try: http://programmers.stackexchange.com/questions/185585/what-is-the-advantage-of-currying
The act of "pinning" arguments to a function's parameters. Differences from currying:
// First, regular left-to-right stuff:
var subFrom10 = sub(10) // currying example
var subFrom10Alt = sub(10,___) // partial application example
subFrom10(6) // 4
subFrom10Alt(6) // 4
// But how do we create a sub10 function?
// We can with right-to-left partial application:
var sub10 = sub(___,10)
sub10(6) // -4
add(1)(2) // 3
add(1,___)(2,___)() // 3
// Assume a "person" function that accepts three
// parameters: first name, last name, age.
person('John', 'Smith', 30)
// { first: 'John', last: 'Smith', age: 30 }
// When dealing with families, repeating the last
// name can be redundant. Wild-cards could help:
var psmith = person(_, 'Smith', _)
[['Sarah', 30], ['Ryan', 32], ['Timmy', 2]].map(psmith)
// [{ first: 'Sarah', last: 'Smith', age: 30 },
// { first: 'Ryan', last: 'Smith', age: 32 },
// { first: 'Timmy', last: 'Smith', age: 2 }
// ]
Learn more in @fogus's tour de force Functional JavaScript in the "Partial Application" section in Chapter 5. Function-Building Functions. It includes many examples, use-cases, diagrams, etc. Note he does not discuss wild-cards as that is a purry-specific feature. Learn more about wild-cards from Purry's own docs.
I am actively working on this project. I hope to release a genuinely useful iteration in 2014. Contributions and collaborations are more than welcomed. See the tests for what is currently possible, they are truth.