Skip to content

ethancarlsson/apcl-fnl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Practical Combinator Library (Fennel)

This is an implementation of A Practical Combinator Library for fennel.

See Combinatory Programming for details.

Use of macros

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 input

The 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.

API

Constants

identity

Returns its argument.

left

Given two arguments, returns the left one.

right

Given two arguments, returns the right one.

Functions

(constant x)

Returns a function which returns x no 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 f to 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 f twice.

(recombine f ...)

Returns a function that applies its arguments to all functions ..., taking the resulting values as the arguments to f.

Note: This is different from the original APCL implementation which accepts only two arguments to f.

(under f g)

Returns a function that applies g to each of its arguments, taking the resulting values as the arguments to f.

identity

Returns its argument.

Kind: global constant

left

Given two arguments, returns the left one.

Kind: global constant

right

Given two arguments, returns the right one.

Kind: global constant

constant

Returns a function which returns x no matter what it is passed.

Kind: global function

compose

Performs function composition.

Any number of functions can be composed.

The innermost function may take any arguments; all subsequent functions expect a single argument.

Kind: global function

apply

Returns a function which applies f to a spread of its argument.

Kind: global function

flip

Returns a function which permutes its arguments and applies them to f.

Kind: global function

duplicate

Returns a function that passes its argument to f twice.

Kind: global function

recombine

Returns a function that applies its arguments to g and h, taking the resulting values as the arguments to f.

Kind: global function

under

Returns a function that applies g to each of its arguments, taking the resulting values as the arguments to f.

Kind: global function

About

This is an implementation of for fennel

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages