Advanced FP for the Enterprise Bee: Optics

Garth Gilmour
Feb 10 · 6 min read
Image for post
Image for post
Sunlight shining through honey

Introduction

This is the sixth article in our exploration of Advanced FP with Kotlin and Arrow. This time we will be exploring the fascinating topic of Optics.

The Case Study

At Instil our training team makes heavy use of JetBrains Space. We create a dedicated instance for each virtual delivery, which gives us the ability to host repositories, mentor via chat channels, post blogs etc… We were featured as a case study by JetBrains at the product launch.

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

The Sample Problem

Let’s say that we need to support alternative formats for describing the content of the Space instance. Perhaps we need to create a Web or Mobile version of our application.

Creating the Entity Classes

Here’s how we might separate things out. For clarity the names of our DSL types are now prefixed with Dsl:

Populating the Data Structure Manually

Our task now is to write code that creates a graph of entity objects, based on an instance of the DSL.

The Need for Optics

This is a general problem in pure functional programming. We want our code to be free of side effects and (hopefully) easy to reason about. Ideally we want our functions to be referentially transparent - whereby any function call can be replaced with the corresponding result. This drives us toward modelling our problem domain with immutable types.

Introducing Optics

Let’s leave our createInstance function alone for now, and start with three simpler examples:

Space Instance - 'Kotlin Programming for MegaCorp - as taught by Instil'
  • The entire structure is copied for us, regardless of the complexity.
Projects:
Project 'Course Examples' (PROJ101) with repos:
http://somewhere.com
Project 'Course Exercises' (PROJ202) with repos:
http://nowhere.com
Profiles:
JANE Jones at Jane.Jones@megacorp.com
PETE Smith at Pete.Smith@megacorp.com

Configuring Optics

In order for this wizardry to work we must do four things:

  • Decorate the entity types with the optics annotation
  • Add a companion object, if one is not already present
  • Use Arrow’s collection wrappers (SetK, ListK and MapK)

Returning to the Problem

Let’s revisit our initial problem of performing a deep copy, from the existing DSL types to the new immutable entity ones. The first thing we can do is replace calls to copy with Lens objects:

Conclusions

The further we go into functional programming, the more we will want to make our data types immutable. The more deeply these types are nested the greater the complexity involved in modifying them. Kotlin provides some support through data classes and copy methods, but a lot of boilerplate code still needs to be introduced.

Thanks

Continuing gratitude to Richard Gibson and the Instil training team for reviews, comments and encouragement on this series of articles. On this occasion extra thanks to Maarten Balliauw, for coaching me last year on the Space design and API. 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