This is an implementation of A Practical Combinator Library for fennel.
See Combinatory Programming for details.
This implementation of apcl includes macros for function composition and recombination as well as normal functions for these combinators.
(import-macros {: compose } :path.to.apcl-macro)
(local apcl (require :path.to.apcl))
(compose f3 f2 f1)
(apcl.compose f1 f2 f3) ; The function returned here will return the same value
; as the one above given the same inputThe macro implementation was done because strangely using a macro can actually
help debugging, being able to use macrodebug to tell how the functions are
composed together is useful when first getting used to the idea of it.
(macrodebug (compose f3 f2 f1)) ; (fn [...] (f3 (f2 (f1 ...))))
(macrodebug (recombine f3 f2 f1)) ; (fn [...] (f3 (f2 ...) (f1 ...)))The other reason to use macros is performance. These macros have to create a table at runtime and loops through it running each function. The impact on performance is probably negligable for most use cases, but it will have some impact.
The function versions are implemented so that the library can be used with lua if you want.
- identity
Returns its argument.
- left
Given two arguments, returns the left one.
- right
Given two arguments, returns the right one.
- (constant x)
Returns a function which returns
xno matter what it is passed.- (compose f ...)
Performs function composition.
Any number of functions can be composed.
The innermost function may take any arguments; all subsequent functions expect a single argument.
- (apply)
Returns a function which applies
fto a spread of its argument.- (flip x y)
Returns a function which permutes its arguments and applies them to
f.- (duplicate x)
Returns a function that passes its argument to
ftwice.- (recombine f ...)
Returns a function that applies its arguments to all functions
..., taking the resulting values as the arguments tof.Note: This is different from the original APCL implementation which accepts only two arguments to
f.- (under f g)
Returns a function that applies
gto each of its arguments, taking the resulting values as the arguments tof.
Returns its argument.
Given two arguments, returns the left one.
Given two arguments, returns the right one.
Returns a function which returns x no matter what it is passed.
Performs function composition.
Any number of functions can be composed.
The innermost function may take any arguments; all subsequent functions expect a single argument.
Returns a function which applies f to a spread of its argument.
Returns a function which permutes its arguments and applies them to f.
Returns a function that passes its argument to f twice.
Returns a function that applies its arguments to g and h, taking the
resulting values as the arguments to f.
Returns a function that applies g to each of its arguments, taking the
resulting values as the arguments to f.
Kind: global function