Advanced FP for the Enterprise Bee: Kleisli

Garth Gilmour
Jan 29 · 4 min read
Image for post
Image for post
An (Non-Kleisli) Arrow Pointing to Honey

Introduction

This is the fourth article in a series, exploring advanced FP concepts for practically minded Kotlin developers. So far we have looked at Traverse, Applicatives and Higher Kinded Types. This time we are investigating the Kleisli type.

A Sample Problem

Let’s start by revisiting an only friend from our first article. This function attempts to read a JVM property, returning the result in an Either.

  • The value of property A will be the key B
  • The value of property B will be the key C
  • This process must be repeated N times
            Vladimir | Jessica
Jessica | Paul
Paul | Ghanima
Ghanima | Moneo
Moneo | Siona

A Procedural Solution

This is how the problem could be solved in a procedural way:

Right(Siona)
  • To implement this function we need to be mindful of the rules for Either
  • Applying this function requires nesting and/or temporary variables

The Kleisli Solution

Here’s the solution written using the Kleisli type. We still wrap up the propertyViaJVM function, but there are important differences:

  • The wrapped function takes a regular String as input
  • The implementation of Either is abstracted from us
Right(Siona)
Left(No JVM property: Alia)

Additional Functionality in Kleisli

So far we have seen that the Kleisli type simplifies the composition of functions, where the functions return, but do not accept, a monadic type like Either or Validated. There are also some helpful utility methods. Let’s modify our input data slightly:

  • Append ‘Atreides’ to the final value produced by the call to run. As you might expect, this ability is provided by the map function.
Right(Siona Atreides)
Left(No JVM property: Alia)

Why Not Use Monads?

When I encountered Kleisli my immediate question was ‘why not just use Monadic Composition?’. It turns out that we always can — and in fact that’s how the Kleisli is implemented.

  • Use the bang operator (or equivalent)
  • Declare variables for intermediate results

What Are Kleisli Arrows?

In both Pure Functional Programming and Category Theory the composition of functions is represented by drawing (or coding) arrows. Here’s a superb talk, filled with arrow diagrams, by Philip Wadler. A Kleisli Arrow represents functional composition within a monadic context. In our case the monad was Either, but it could have been Validated, Writer, State etc...

Conclusions

Despite the term Kleisli originating from Category Theory, it is neither complex nor impractical. Kleisli is a great alternative to Monadic Composition any time you have:

  • A requirement to chain these functions together to obtain a final result.

Running The Sample Code

As usual all the code listed can be found in this BitBucket repo. However the Kleisli type used is still experimental and hence can be found in the Arrow Incubator Project. To run my ‘UsingKleisli’ project you will have to first clone and build arrow-incubator, and then copy the arrow-mtl and arrow-mtl-data libraries into the libs folder.

Thanks

I am grateful to Richard Gibson and the Instil training team for reviews, comments and encouragement on this series of articles. All errors are of course my own.

Google Developers Experts

Experts on various Google products talking tech.

Garth Gilmour

Written by

Helping developers develop software better. Coding for 30 years, teaching for 20. Google Developer Expert. Trainer at Instil. Also martial arts and philosophy.

Google Developers Experts

Experts on various Google products talking tech.

Garth Gilmour

Written by

Helping developers develop software better. Coding for 30 years, teaching for 20. Google Developer Expert. Trainer at Instil. Also martial arts and philosophy.

Google Developers Experts

Experts on various Google products talking tech.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store