Nest.JS | Monads -> State

The State monad is a functional programming concept that allows you to manage state in a functional, immutable way. It allows you to write code that updates the state of a computation in a pure, composable way, which makes it easier to reason about and test.

The State monad is often used to manage stateful computations in a functional programming language, such as JavaScript. The State monad allows you to describe a computation that updates some state in a pure, composable way, which makes it easier to reason about and test your code.

An State monad is typically implemented as an object that has a single method, called run, which takes the initial state as an input and returns the result of the computation and the updated state. The State monad can be used to chain together multiple computations that update the same state and create more complex computations.

Here’s an example of the State monad in JavaScript:

class State {
  constructor(fn) {
    this.run = fn
  }
  map(f) {
    return new State((state) => {
        const [result, updatedState] = this.run(state);
        return [f(result), updatedState];
    });
  }
  chain(f) {
    return new State((state) => {
        const [result, updatedState] = this.run(state);
        return f(result).run(updatedState);
    });
  }
  // ...
}

In this example, the State class takes a function fn in its constructor, and has methods run, map, and chain that allow you to pass the initial state, transform the result, and chain multiple computations together.

For example, you could create a State that takes an initial number and returns a new number incremented by one, you could then chain multiple of these States together to create a more complex computation that updates the state multiple times, without having to manually pass the state through each step.

It’s important to note that the State Monad is not a common pattern in JavaScript, it’s mainly used in functional programming languages like Haskell, scala, etc.


Here’s an example of how you could use the State Monad in Nest.js:

import { Injectable } from '@nestjs/common';
import { State, state } from 'fp-ts/lib/State';

interface StateType {
  counter: number;
}

@Injectable()
export class MyService {
  public incrementCounter(): State<StateType, number> {
    return state.get().map(s => s.counter + 1).put({ counter: s.counter + 1 });
  }

  public decrementCounter(): State<StateType, number> {
    return state.get().map(s => s.counter - 1).put({ counter: s.counter - 1 });
  }

  public getCounter(): State<StateType, number> {
    return state.get().map(s => s.counter);
  }
}

In this example, the MyService class has three methods incrementCounter(), decrementCounter() and getCounter() that returns an instance of the State Monad.

The State Monad is used to manage state in a functional way, this example uses a simple counter as a state, but the state could be anything like an object or an array.

The incrementCounter() method increments the counter and updates the state, the decrementCounter() method decrements the counter and updates the state, the getCounter() method returns the current counter value.

You can chain multiple computations using the .map(), .chain(), .ap() methods.const program:

const program: State<StateType, number> = myService
  .incrementCounter()
  .chain(() => myService.incrementCounter())
  .chain(() => myService.getCounter());

console.log(program.run({ counter: 0 }).value);

In this example, the program increments the counter twice and returns the current counter value.

It’s important to note that this example is a basic one and doesn’t demonstrate the full power of Monads in functional programming. Monads can be used to handle various cases like error handling, chaining multiple computations and more, it’s best to understand the problem and use cases before applying monads.

Also, the run() method must be called in order to execute the State Monad and get the results, it takes an initial state as an argument.