Description Usage Arguments Value Note Author(s) See Also Examples
It allows to compose two functions of the form f(x, ret)
and g(x, ret)
returning a function h(x,ret)
which is the composition f . g.
It implements the composition operator of the Continuation category.
The the composition has the following properties:
Associativity: h . (f . g) = ( h . g) . f
Unity: f . identity2 = f = identity2 . f
In order for these relations to hold, the function f
and g
must not deal with global mutable states.
1 | compose(f, g)
|
f |
The first function that must be composed |
g |
The first function that must be composed |
Rerturns the composite function of f
and g
The composition is performed from left to right i.e. such that the first function executed is f
.
Matteo Provenzano
http://www.alephdue.com
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # Example 1
# define an arrow in the Continuation category.
# this function applies the continuation to the
# increment of its argument and then decrements it.
one <- function(x, ret) {
return(ret(x+1) - 1)
}
# define another arrow in the Continuation category.
# this function doubles its argument.
two <- function(x, ret) {
return(ret(2*x))
}
# create the composition
# this is exactly the same as one %.% two
composite <- compose(one, two)
# build the function (forget the continuation)
execute1 <- forget(composite)
execute1(1)
# returns 3
# Example 2
# compose the function further to loop over an array of elements
# lapply and sapply are already arrow in the Continuation category
loop <- compose(lapply, composite)
# build the function
execute2 <- forget(loop)
execute2(1:10)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.