Combinators
#
applyTocrocks/combinators/applyTo
Ever run into a situation where you have a value but do not have a function to apply it to? Well this little bird, named Thrush, is there to help out. Just give it a value and it will give you back a function ready to take a function. Once that function is provided, it will return the result of applying your value to that function.
- Source
- Runkit
#
compose2crocks/combinators/compose2
compose2
allows for composition between a binary
function and
two unary
functions. compose2
takes a binary
function followed by
two unary
functions and returns a binary
function that maps the first
argument with the first unary
and the second with the second, passing
the results to the given binary
and returning the result.
- Source
- Runkit
#
composeBcrocks/combinators/composeB
Provides a means to describe a composition between two functions. it takes two
functions and a value. Given composeB(f, g)
, which is read f
after g
, it
will return a function that will take value a
and apply it to g
, passing the
result as an argument to f
, and will finally return the result of f
. This
allows only two functions, if you want to avoid things like:
composeB(composeB(f, g), composeB(h, i))
then check
out compose
.
- Source
- Runkit
#
constantcrocks/combinators/constant
This is a very handy dandy function, used a lot. Pass it any value and it will
give you back a function that will return that same value no matter what you
pass it.
constant
is perfect for those moments where you need to pass a function but
do not care about the input. constant
will swallow any value given to it and
always return the initial value it was given.
It is important to note that any function that is passed into constant
will
get the added benefit of having curry
applied to it.
- Source
- Runkit
#
convergecrocks/combinators/converge
Provides a means of passing an acculumating function and two branching functions. A value can be applied to the resulting function which will then be applied to each branching function, the results of which will be applied to the accumulating function.
- Source
- Runkit
#
flipcrocks/combinators/flip
This little function just takes a function and returns a function that takes
the first two parameters in reverse. flip
is perfectly suited for those
moments where you have the context of your function but not the data.
Applying flip
to the function will allow you to pass in your context and will
return a function waiting for the data. This will happen often when you're
using composition.
When required, one can compose flip calls down the line to flip all, or some of the other parameters if there are more than two. Mix and match to your heart's desire.
- Source
- Runkit
#
identitycrocks/combinators/identity
This function and constant
are the workhorses of writing code
with this library. It quite simply is just a function that when you pass it
something, it returns that thing right back to you. So simple, I will leave it
as an exercise to reason about why this is so powerful and important.
#
psicrocks/combinators/psi
psi
is a function that can be considered the sister of converge
.
Where converge
takes one argument and maps it through
two unary
functions, merging the resulting values with a binary
function, psi
takes two arguments and runs them each through the
same unary
function before merging them with the given binary function.
psi
is often used to compose
equality checking functions
or when needing to validate two arguments of the same type.
- Source
- Runkit
#
substitutioncrocks/combinators/substitution
While it may seem like a complicated little bugger, substitution
can come in
very handy from time to time. substitution
is used when you have a binary
function
and you can supply the first argument and can use that value to create the
second argument. It first takes a binary
function followed by a unary
function
for it's first two arguments. This will return a function that is ready to take
some context, a
. Once supplied the fun starts, it will pass the given a
to
the binary
and unary
functions, and will then apply the result of
the unary
function as the second parameter of the binary
function. Finally
after all that juggling, it will return the result of that binary
function.
When used with partial application on that first parameter, a whole new world of combinatory madness is presented!
- Source
- Runkit