A Moore machine is a comonadic data structure that holds a state and, in order to change it, we need to dispatch events of some specific type. This approach is similar to the Elm architecture or Redux.

For creating a Moore machine, we need its initial state and a handle function that will determine the inputs it can accept and how the state will change with each one.

import arrow.core.*
import arrow.ui.*

fun handleRoute(route: String): Moore<String, Id<String>> = when (route) {
  "About" -> Moore(Id("About"), ::handleRoute)
  "Home" -> Moore(Id("Home"), ::handleRoute)
  else -> Moore(Id("???"), ::handleRoute)

val routerMoore = Moore(Id("???"), ::handleRoute)

// About

We also have an extract function that returns the current state, and a coflatMap that transforms its type:

    .coflatMap { (view) ->
      when (view.extract()) {
        "About" -> 1
        "Home" -> 2
        else -> 0
// 0

Do you like Arrow?

Arrow Org