State Monad Unlocked

State Monad:

Let’s do more.

  • StateT is a monad transformer. It’s not important what it does here, except that the stateMonad function on StateT generates a State monad, with Generator as the state.
  • The import in the second line makes available the various combinators within the State monad as well as the generic Monad abstraction. (Remember, State is also a monad.)
  • modify is the combinator you used earlier that takes a function to modify the state. In our case, it’s creating another instance of Generator that will generate another account number.
  • gets applies the transformation function passed to it to the value and returns the updated State monad. So here it checks whether the generated account number already exists. As long as exists returns true, you need to execute the body of whileM_.
  • If you want to execute the computation and generate the next valid account number, you need to pass a start configuration of Generator and fire an exec on the monad.

--

--

--

A software engineer and a functional programming enthusiast at Simple-machines, Sydney, and a hardcore hiking fan. https://twitter.com/afsalt2

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

101 advice for backend engineers

Login with google

From Player to Pawn? One Engineer’s Perspective (Part 2)

GraphQL at our Museum: No REST for the Wicked

Developing the GMF model to add a new mediator in WSO2 EI Tooling -Part 5

Natural Language Processing for Developers

How to start a career as a full-stack developer, what is an “ideal stack” and which technological…

Django + Celery Setup on Microsoft Azure

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
Afsal Thaj

Afsal Thaj

A software engineer and a functional programming enthusiast at Simple-machines, Sydney, and a hardcore hiking fan. https://twitter.com/afsalt2

More from Medium

Polymorphic functions in Scala

Reactive Web Application: With Play, Akka, and Reactive Streams — Part 1

Calculating Moving Averages using Akka Streams — Part 2

Clear and concise logging in Scala.