gare functions and
xis the value being "piped" through them.
gwill run before the
f, creating a right to left flow of data. This is much more readable than nesting a bunch of function calls. Without compose, the above would read:
reversewill turn the list around while
headgrabs the initial item. This results in an effective, albeit inefficient,
lastfunction. The sequence of functions in the composition should be apparent here. We could define a left to right version, however, we mirror the mathematical version much more closely as it stands. That's right, composition is straight from the math books. In fact, perhaps it's time to look at a property that holds for any composition.
compose, the result will be the same. That allows us to write a variadic compose and use it as follows:
angry. If familiar with Fowler's "Refactoring", one might recognize this process as "extract function"...except without all the object state to worry about.
replace? What we're doing is piping our data through each function of 1 argument. Currying allows us to prepare each function to just take its data, operate on it, and pass it along. Something else to notice is how we don't need the data to construct our function in the pointfree version, whereas in the pointful one, we must have our
wordavailable before anything else.
map, a function of two arguments, without first partially applying it.
toLowersince it's working on an array.
tracefunction allows us to view the data at a certain point for debugging purposes. Languages like Haskell and PureScript have similar functions for ease of development.
Object, etc. We often view data types as sets of all the possible values. One could look at
Booleanas the set of
Numberas the set of all possible numeric values. Treating types as sets is useful because we can use set theory to work with them.
compose. We've discussed that our
composefunction is associative which is no coincidence as it is a property that must hold for any composition in category theory.
id. This function simply takes some input and spits it back at you. Take a look:
idmust play nicely with compose. Here is a property that always holds for every unary (unary: a one-argument function) function f:
idused all over the place soon, but for now we see it's a function that acts as a stand in for a given value. This is quite useful when writing pointfree code.
>=as morphisms (actually any partial or total order can be a category). There are heaps of categories, but for the purposes of this book, we'll only concern ourselves with the one defined above. We have sufficiently skimmed the surface and must move on.
compose()to rewrite the function below.
averageDollarValueas a composition.