Modify The State Of A State Monad

Share this video with your friends

Send Tweet

Using put to update our state for a given state transaction can make it difficult to modify a given state based on its previous value. In this lesson we will explore a means to lift specialized functions that can be used modify our state’s value. These specialized functions must have the same type in their input and output. We take a look at a construction helper named modify that is used to remove the boilerplate of this type of construction. We also start to see how we can extend simple discrete stateful transactions to create other stateful transactions.

Aaron
~ 6 years ago

is there a lesson series that explains what Monads are? I feel so lost by all of these terms and the current docs of crocks is not suited for a novice.

Ian Hofmann-Hicksinstructor
~ 6 years ago

There is a really good course on egghead that lets you build an intuition on these datatypes.

https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript

State is just one of many datatypes, and the "labels" like: Functor, Applicative and Monad are ways to describe properties about these datatypes.

Most of the datatypes used out there (Maybe, Either, Async (Future), State) abstract away or encapsulate some sort of effect, behavior or structure.

Maybe, for instance, is a way to represent disjunction (logical or) with the false side of the disjunction all mapping to a singleton type (like undefined or null).

The effect that State provides is the ability to either read from or modify a shared state.

For us programmers/developers/etc, The fact that something is a Monad means that we have a way to combine the effects of (2) instances of that datatype. In JS (using the fantasy-land spec), this means that a datatype will have a chain method and an of static function on the constructor. So when we chain a function, it will combine the effects.

For something like Maybe, that may be thought of as nested ifs without else cases. For State, chaining allows use to combine multiple reads/writes to the provided state.

While that may make no sense now, it is something to keep in mind as you go through Brian's course.