Advanced FP for the Enterprise Bee: Applicatives

Garth Gilmour
Jan 15 · 5 min read
Image for post
Image for post
Containers of Honey

Introduction

In the first article of this series we showed how useful the traverse operator can be. As part of this we used an Applicative without further explanation. In this article we will explain Applicatives from scratch, and then circle back to the original example. All the code for this series is available in this repository.

The Sample Problem

Just like last time, let’s say our teams resident FP super-fan has been hard at work. This time they have converted a UI helper function to return an Either.

What's your name?
Lucy
Where do you live?
Melbourne
Hello Lucy from Melbourne

Introducing Applicatives

Here’s a much nicer way to solve the problem. It requires we write a special function, which I’ve called action.

val action = { name: String -> { location: String -> "Hello $name from $location" } }
  • This second lambda takes a parameter called location and returns a string
  • The string includes the values of both name and location
val result = location.ap(name.map(action))
What's your name?
Jane
Where do you live?
Bristol
Hello Jane from Bristol

Explaining the Applicative

In essence an Applicative provides a way to manipulate the content of two or more instances of a container type, without having to resort to procedural shenanigans. The ap function is the most commonly used, but there are a number of other operators for creating and combining instances.

fun <A, B, C> EitherOf<A, B>.ap(ff: EitherOf<A, (B) -> C>): Either<A, C> =
flatMap { a -> ff.fix().map { f -> f(a) } }

Revisiting Traverse

If you remember from last time, this was how we called the traverse operation:

val result = input.traverse(Either.applicative(), ::propertyViaJVM)

Applicatives and Validated

As in the previous article we can improve the original code by switching from Either to Validated. The Validated type adds the extra functionality of combining error messages.

What's your name?
123
Where do you live?
456
Sorry: 123 does not match [A-Z a-z]+456 does not match [A-Z a-z]+
What's your name?
123
Where do you live?
456
Sorry: 123 does not match [A-Z a-z]+ , 456 does not match [A-Z a-z]+

Conclusions

In this article we explored the Applicative type and the ap operator. Along the way we also saw what a Semigroup is. Next time round we will resolve the other outstanding issue, namely the fix method and Higher Kinded Types. Stay tuned…

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