Monad/State.bruijn


# MIT License, Copyright (c) 2024 Marvin Borner
# see samples/fun/rng-state for example usage

:import std/Combinator .

# scala: s0 ⇒ { (s1, a) = run(s0); (s1, f(a)) }
map [[[1 0 [[[0 2 (5 1)]]]]]] ⧗ (a → b) → (State s a) → (State s b)

# monadic bind/flat-map operator
# scala: s0 ⇒ { (s1, a) = run(s0); f(a).run(s1) }
bind [[[2 0 [[3 0 1]]]]] ⧗ (State s a) → (a → (State s b)) → (State s b)

…>>=… bind

pure [[[0 2 1]]] ⧗ a → (State s a)

:test ((w' ∘ c) >>= [(w' ∘ c) >>= [pure 0]] [[0]]) (w' [[0]])
:test ((w' ∘ c) >>= [(w' ∘ c) >>= [pure 0]] [[1]]) (w' [[1]])