Beautiful Functions: Compose

I’d like to take a look at some functions whose form and function are the epitome of elegant.

The B combinator, sometimes called “compose”:

const B = (f) => (g) => (x) => f(g(x))

Which has the type:
(b -> c) → (a -> b) → a → c…


This content originally appeared on DEV Community and was authored by Jethro Larson

I'd like to take a look at some functions whose form and function are the epitome of elegant.

The B combinator, sometimes called "compose":

const B = (f) => (g) => (x) => f(g(x))

Which has the type:
(b -> c) → (a -> b) → a → c

in TypeScript:

const B = <A, B, C>(g: (y: B) => C) =>
  (f: (x: A) => B) =>
    (a: A): C =>
      g(f(a));

What it does is combine two unary functions (single-argument functions) together such that the output of the second is the input of the first. This is the core of composing functions in math as well as programming. If you have a couple procedures you want to link and sequence consider using this operator to do so.

const armTheMissiles = (missles: Missle[]): Missle[] => {...}

const fireTheMissles = (missles: Missle[]): void => {...}

const armAndFireMissles = B(fireTheMissles)(armTheMissles)

This is such a core way of writing code that advanced languages like Haskell and F# have operators dedicated to it: . and <<, respectively.

armAndFireMissles = fireTheMissles . armTheMissles

An easy place to integrate this function into your code is in cases where you take a callback that calls a function with it's parameters.

const fetchStuff = () =>
  fetch(...).then(data => parseData(validate(data))

In this case you can use the B combinator to drop the inner lambda:

const fetchStuff = () =>
  fetch(...).then(B(parseData)(validate)

This way of eliminating lambdas by using composition techniques is called eta-reduction.

It may help to have an idea of what a partially applied B means. I like to think of it as the function has its arms out and is ready for the conga line.

const congaReadyFoo = B(foo);
const congaReadyBar = B(bar);
const congaLine = congaReadyFoo(congaReadyBar(baz));
// where foo, bar, and baz are all unary functions with compatible inputs and outputs.

That all said, it's easy to go too far with this kind of technique.

// probably too far
const congaLine = B(foo)(B(bar)(baz))

This is mainly due to the syntax being hard to follow though as when it's an operator it's much easier:

congaLine = foo . bar . baz 

There's many more fun combinators but I wanted to start here to start somewhere.

Bonus fact: B turns functions themselves into a functor

class Functor f where
  fmap :: (a -> b) -> f a -> f b

instance Functor ((->) r) where
  fmap = (.)

That is, given a function from a -> b, our compose function takes a function which takes something of type a and returns a function which takes something of type b. This means it's totally cool to think of composition as mapping a function over another function.


This content originally appeared on DEV Community and was authored by Jethro Larson


Print Share Comment Cite Upload Translate Updates
APA

Jethro Larson | Sciencx (2021-12-30T23:15:08+00:00) Beautiful Functions: Compose. Retrieved from https://www.scien.cx/2021/12/30/beautiful-functions-compose/

MLA
" » Beautiful Functions: Compose." Jethro Larson | Sciencx - Thursday December 30, 2021, https://www.scien.cx/2021/12/30/beautiful-functions-compose/
HARVARD
Jethro Larson | Sciencx Thursday December 30, 2021 » Beautiful Functions: Compose., viewed ,<https://www.scien.cx/2021/12/30/beautiful-functions-compose/>
VANCOUVER
Jethro Larson | Sciencx - » Beautiful Functions: Compose. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/30/beautiful-functions-compose/
CHICAGO
" » Beautiful Functions: Compose." Jethro Larson | Sciencx - Accessed . https://www.scien.cx/2021/12/30/beautiful-functions-compose/
IEEE
" » Beautiful Functions: Compose." Jethro Larson | Sciencx [Online]. Available: https://www.scien.cx/2021/12/30/beautiful-functions-compose/. [Accessed: ]
rf:citation
» Beautiful Functions: Compose | Jethro Larson | Sciencx | https://www.scien.cx/2021/12/30/beautiful-functions-compose/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.