The List Monad

class hymn.types.list.List(value)

Bases: hymn.types.monadplus.MonadPlus

the list monad

nondeterministic computation

fmap(f)

return list obtained by applying f to each element of the list

join()

join of list monad, concatenate list of list

plus(other)

concatenate two list

hymn.types.list.fmap(f, iterable)

fmap works like the builtin map, but return a List instead of list

hymn.types.list.list_m

alias of List

hymn.types.list.unit

the unit of list monad, an empty list

hymn.types.list.zero

the zero of list monad, an empty list

Hy Specific API

list-m

alias of List

Reader Macro

* [seq]

turn iterable seq into a List

Examples

Do Notation

=> (require hymn.dsl)
=> (import [hymn.types.list [list-m]])
=> (list (do-monad [a (list-m [1 2 3])] (inc a)))
[2, 3, 4]
=> (list (do-monad [a (list-m [1 2 3]) b (list-m [4 5 6])] (+ a b)))
[5, 6, 7, 6, 7, 8, 7, 8, 9]
=> (list (do-monad [a (list-m "123") b (list-m "xy")] (+ a b)))
['1x', '1y', '2x', '2y', '3x', '3y']

Do Notation with :when

=> (require hymn.dsl)
=> (import [hymn.types.list [list-m]])
=> (list (do-monad
...         [a (list-m [1 2 4])
...          b (list-m [1 2 4])
...          :when (!= a b)]
...         (/ a b)))
[0.5, 0.25, 2.0, 0.5, 4.0, 2.0]

Operations

=> (require hymn.dsl)
=> (import [hymn.types.list [fmap list-m]])
=> (instance? list-m (fmap inc [0 1 2]))
True
=> (for [e (fmap inc [0 1 2])] (print e))
1
2
3

Reader Macro

=> (require hymn.types.list)
=> (import [hymn.types.list [list-m]])
=> (instance? list-m #*[0 1 2])
True
=> (list (do-monad [a #*(range 10) :when (odd? a)] (* a 2)))
[2, 6, 10, 14, 18]