The State Monad

class hymn.types.state.State(value)

Bases: hymn.types.monad.Monad

the state monad

computation with a shared state

bind(f)

the bind operation of State

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

evaluate(s)

evaluate state monad with initial state and return the result

execute(s)

execute state monad with initial state, return the final state

run(s)

evaluate state monad with initial state, return result and state

classmethod unit(a)

the unit of state monad

hymn.types.state.state_m

alias of State

hymn.types.state.lookup(key)

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

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(**keys/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

hymn.types.state.unit()

alias of State.unit()

hymn.types.state.evaluate()

alias of State.evaluate()

hymn.types.state.execute()

alias of State.execute()

hymn.types.state.run()

alias of State.run()

hymn.types.state.get_state

return the current state

hymn.types.state.gets(f)

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

Hy Specific API

state-m

alias of State

Functions

<-

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]])
=> (.run (do-monad [a (gets first)] 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 first) [1 2 3])
(1, [1, 2, 3])
=> (.run (gets second) [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 inc) 41)
(41, 42)
=> (.evaluate (modify inc) 41)
41
=> (.execute (modify inc) 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 inc) [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])