The State Monad

class hymn.types.state.State

the state monad

computation with a shared state

bind(self, f)

the bind operation of State

use the final state of this computation as the initial state of the second”

classmethod unit(cls, a)

the unit of state monad

evaluate(self, s)

evaluate state monad with initial state and return the result

execute(self, s)

execute state monad with initial state, return the final state

run(self, s)

evaluate state monad with initial state, return result and state

hymn.types.state.get_state()

return the current state

hymn.types.state.lookup(key)

return a monadic function that lookup the vaule with key in the state

hymn.types.state.gets(f)

gets specific component of the state, using a projection function f

hymn.types.state.modify(f)

maps the current state with f to a new state inside a state monad

hymn.types.state.set_state(s)

replace the current state and return the previous one

hymn.types.state.set_value(key, value)

return a monadic function that set the vaule of key in the state

hymn.types.state.set_values(**values)

return a monadic function that set the vaules of keys in the state

hymn.types.state.update(key, f)

return a monadic function that update the vaule by f with key in the state

hymn.types.state.update_value(key, value)

return a monadic function that update the vaule with key in the state

Hy Specific API

state-m

alias of State

<-

alias of lookup()

<-state
get-state

alias of get_state()

state<-
set-state

alias of set_state()

set-value

alias of set_value()

set-values

alias of set_values()

update-value

alias of update_value()

Examples

Do Notation

=> (import hymn.types.state [gets])
=> (require hymn.macros [do-monad-return])
=> (.run (do-monad-return [a (gets (fn [x] (get x 0)))] a) [1 2 3])
#(1 [1 2 3])

Operations

Use get-state() to fetch the shared state, <-state is an alias of get-state()

=> (import hymn.types.state [get-state <-state])
=> (.run get-state [1 2 3])
#([1 2 3] [1 2 3])
=> (.run <-state [1 2 3])
#([1 2 3] [1 2 3])

Use lookup() to get the value of key in the shared state, <- is an alias of lookup()

=> (import hymn.types.state [lookup <-])
=> (.run (lookup 1) [1 2 3])
#(2 [1 2 3])
=> (.evaluate (lookup 1) [1 2 3])
2
=> (.evaluate (lookup 'a) {'a 1 'b 2})
1
=> (.run (<- 1) [1 2 3])
#(2 [1 2 3])
=> (.evaluate (<- 1) [1 2 3])
2
=> (.evaluate (<- 'a) {'a 1 'b 2})
1

gets() uses a function to fetch value of shared state

=> (import hymn.types.state [gets])
=> (.run (gets (fn [x] (get x 0))) [1 2 3])
#(1 [1 2 3])
=> (.run (gets (fn [x] (get x 1))) [1 2 3])
#(2 [1 2 3])
=> (.run (gets len) [1 2 3])
#(3 [1 2 3])

modify() changes the current state with a function

=> (import hymn.types.state [modify])
=> (.run (modify (fn [x] (+ x 1))) 41)
#(41 42)
=> (.evaluate (modify (fn [x] (+ x 1))) 41)
41
=> (.execute (modify (fn [x] (+ x 1))) 41)
42
=> (.run (modify str) 42)
#(42 "42")

set-state() replaces the current state and returns the previous one, state<- is an alias of set-state()

=> (import hymn.types.state [set-state state<-])
=> (.run (set-state 42) 1)
#(1 42)
=> (.run (state<- 42) 1)
#(1 42)

set-value() sets the value in the state with the key

=> (import hymn.types.state [set-value])
=> (.run (set-value 2 42) [1 2 3])
#([1 2 3] [1 2 42])

set-values() sets multiple values at once

=> (import hymn.types.state [set-values])
=> (.run (set-values :a 1 :b 2) {})
#({} {"a" 1  "b" 2})

update() changes the value with the key by applying a function to it

=> (import hymn.types.state [update])
=> (.run (update 0 (fn [x] (+ x 1))) [0 1 2])
#(0 [1 1 2])

update-value() sets the value in the state with the key

=> (import hymn.types.state [update-value])
=> (.run (update-value 0 42) [0 1 2])
#(0 [42 1 2])