Advanced FP for the Enterprise Bee: Traverse

Garth Gilmour
Jan 8 · 6 min read
Image for post
Image for post
Honey Bees in Hive

Introduction

This article is the first in a series, where we introduce advanced FP concepts via Kotlin. The intended audience is Enterprise Developers like myself, the Worker Bees of our industry.

Setting the Stage

What do marathon running, music, martial arts and programming all have in common? They all have ‘exit points’ where large numbers of participants drop out. Typically exit points occur when a meaningful goal has been met, but the distance to the next is intimidating.

  1. The standard FP Toolkit (flatMap, reduce, partition etc…)
  2. Custom higher order functions, currying and partial invocation
  3. Functional types and composition (Option, Either, Validated etc…)
  4. Here be Dragons…

Introducing Traverse

Let’s say you have a colleague, who has recently been on a Kotlin course with an [ahem] highly skilled and friendly instructor. As part of this they were exposed to the Arrow framework and its functional data types. One day you come across this in the codebase:

[Right(test1.txt), Right(test2.txt), Right(test3.txt)]
[Right(test1.txt), Left(No JVM property: false), Right(test3.txt)]
  • A Left containing an error message

Traverse in Action

Here’s an implementation that does what we want:

Results are:
test1.txt
test2.txt
test3.txt
No JVM property: false
No JVM property: false
No JVM property: false

Investigating Our Success

Huzzah! So the purpose of traverse is to invert the types we are working with.

  • We need to apply an action to each value
  • The action returns a Foo container
  • We want a Foo<List>, not a List<Foo>
  • The calls to fix are necessary to perform type conversions. Behind the scenes Arrow is emulating something called Higher Kinded Types.

Traverse and Files

Let’s say our functional friend is also in charge of the code to retrieve values from a properties file:

Results are:
test1.txt
test2.txt
test3.txt
No property called false
No property called false
No property called false
No file called false.properties

Enter the Monad

Because Either is a Monad we can compose calls together in a very neat way.

  1. The value of each of these properties will itself be a key used in a properties file
  2. The values held in the properties file are names of text files containing data
  3. We want to read and output all the data
cagney.lacy=test1.txt
starsky.hutch=test2.txt
hart.hart=test3.txt

Switching to Validated

In all our code so far we have been using the Either type to represent a result or an error message. This is perfectly legitimate, but the Validated type is intended for precisely this purpose. So we could rewrite our existing code to use it instead.

Conclusions

Hopefully this article has added traverse to your FP arsenal, and wetted your appetite to know how else FP can simplify your code. That’s what I intend to cover. But before that we need to consolidate what has been achieved so far.

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