Advanced FP for the Enterprise Bee: State

Garth Gilmour
Feb 19 · 6 min read
Image for post
Image for post
Bees Collaborating to Build Honeycomb

Introduction

Welcome to the penultimate article in our eight part series, examining advanced functional programming for the practically minded enterprise developer. This article will expand on the use case from the previous article, where we developed a set of immutable types to configure an instance of JetBrains Space. On this occasion we are exploring the world of Monads, and in particular the State type. As always the sample code is available in this repo.

The House Of ‘M’

Just like in the comics, the House Of M is a scary place in software engineering. It’s a disconcerting alternate reality - where alien types with strange names and mysterious abilities seem to have taken control. Fortunately the reality is much more banal.

Image for post
Image for post
  • If there might be many orders we use a collection, such as List<Order>
  • To manage errors we could use a Try<Order> or Either<Error, Order>
  • In the case where, for some orders, only the total value was available we could use an Either<Double, Order>
  • If the order was subject to a series of checks, any of which could fail, we might use a Validated<List<String>, Order>
  • To postpone querying the database, we could wrap our query code in an Eval or an IO. We might do this in order to batch queries for performance.

Introducing the State Monad

The State type is used with functions which:

  • Return both a modified version of the state and a result.
Kotlin Programming for MegaCorp
Profiles are:
Pete Smith at Pete.Smith@megacorp.com
Jane Jones at Jane.Jones@megacorp.com
Projects are:
Project 'Course Exercises' (PROJ202) with repos:
http://elsewhere.com
Project 'Course Examples' (PROJ101) with repos:
http://somewhere.com
Blogs are:
Setting Up
setup.md
More client-specific content
Welcome to the Course
welcome.md
Some additional client-specific content

Examining the Display Functions

Here’s the simplest of the display functions:

Using State to Build the Instance

The previous example showed using the State type, plus monadic composition, to output a description of our Instance. However we were not modifying the state at any point.

Created Projects Created Profiles Created Blogs
Kotlin Programming for MegaCorp
Profiles are:
Pete Smith at Pete.Smith@megacorp.com
Jane Jones at Jane.Jones@megacorp.com
Projects are:
Project 'Course Exercises' (PROJ202) with repos:
http://elsewhere.com
Project 'Course Examples' (PROJ101) with repos:
http://somewhere.com
Blogs are:
Setting Up
setup.md
More client-specific content
Welcome to the Course
welcome.md
Some additional client-specific content

Conclusions

We have been using Monads all the way through this series of articles, but without making special mention of it. A Monad is a container that wraps around one or more values to provide extra functionality. Monadic types like Either, Validated and State are provided by functional libraries like Arrow (Kotlin) and Cats (Scala). Although every Monad is a Container not every Container is a Monad. For example Future is not Monadic because it is non-deterministic.

Thanks

Continuing gratitude to Richard Gibson and the Instil training team for reviews, comments and encouragement on this series of articles.

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