Chainable Currying
curry-chain is a library for creating literary, chai-style curried functions.
Usage
curry-chain allows to define curried functions: functions with settable arguments and chainable setters. Curried functions are wrapped around ordinary functions, allowing to set their arguments one by one, set individual properties for object arguments, and much more.
In order to create a curried function, call curry
on an existing function:
const curry = ; { return how + ', ' + who + '!';} var curryGreet = currywhere hasand has ;
This creates a chained function, the 0-th argument of which can be set with
a who
setter, and the 1-st with a how
setter:
var greeting = curryGreet;// greeting === 'Hello, universe!'
More details of how the above code works:
fn
sets the function for curryingarg
selects the context - the argument index, which furthersetter
and other instructions will describesetter
creates a chainable setter with the specified name. The setter will set the current argument. By default, a setter replaces the argument, but other behaviors are also availabledone
signals that the function is completewhere
,that
andand
are language chains, they are here just to improve readablility, just like inchai
The created curried function will have the specified chainable setters, and the done
function, which will call the original function specified by fn
,
with the values of arguments set.
Notice that curry
looks like a curried function: it is chainable and has
the done
terminator. This is not a coincidence; curry
actually is a curried
wrapper around a low-level curry creator function (see the source code
for more details).
Argument assignment
Several setters
You can specify several setters for the same argument:
var curryGreet = currywhere has hasand ; curryGreet;curryGreet;
Language
You can specify language chains to improve readablility by using the language
chain:
var curryAdd = currywhere hasand hasand language'with' 'and' ; curryAddwithx10andy5;
The names of language chains may coincide with names of setters.
Setters for properties
It is possible to specify setters for separate properties of an argument
using the option
and options
chains:
{ // uses pos.x, pos.y, options.color and options.size} var curriedDraw = currywhere has hasandand hasoptions'color' 'size' language'with' 'and' ; curriedDrawshape'circle' withx10andy25 color'blue'andsize10;
Option sinks
The sink
chain specifies a setter that can assign several properties of an argument
using Object.assign
:
var curriedDraw = // ... has; // 'with' may work simultaneously as a language chain and a sinkvar shape = curriedDrawshape'circle'withx10andcolor'blue';// Leaves { color: 'blue' } option intactshape;
Defaults
You can specify default argument values by using the defaultsTo
chain:
var curriedDraw = // ... ;
Partial assignment
As it can be expected from currying, curried functions may have only some arguments assigned.
var curryAdd = // ...1 2 3; // returns [11, 12, 13]
Immutability
All generated curry chains are immutable, so you can safely pass them around.
var curriedDraw = // ...var circle = curriedDrawtype'circle';var shapesizesz; // will draw red circle with size 10var sizedCircle = ;// draws a blue circle with size 25;// still draws a red circle with size 10sizedCirclex5y10;
Functional interface
curry
is implemented using objects. There is also a purely functional implementation,
available via require('chain-curry/fn')
. Unlike the default implementation,
the functional one does not require the done
terminator (although it still works):
var curry = ; var curryAdd = currywhere hasand hasand language'with' 'and'; 1 2 3; // returns [11, 12, 13]
Unfortunately, the functional implementation is ~2 times slower than the object one. This is because the object implementation can use prototypes in order to reuse setters during chaining; the functional one needs to create new functions for setters during each chained call.
License
curry-chain is available under the terms of the Apache 2.0 license.