The Maybe Monad

hymn.types.maybe - the maybe monad

class hymn.types.maybe.Just(value)

Bases: hymn.types.maybe.Maybe

Just of the Maybe

class hymn.types.maybe.Maybe(value)

Bases: hymn.types.monadplus.MonadPlus, hymn.types.monoid.Monoid, hymn.mixins.Ord

the maybe monad

computation that may fail

append(other)

the append operation of Maybe

bind(f)

the bind operation of Maybe

apply function to the value if and only if this is a Just.

from_maybe(default)

return the value contained in the Maybe

if the Maybe is Nothing, it returns the default values.

classmethod from_value(value)

wrap value in a Maybe monad

return a Just if the value is evaluated as True. Nothing otherwise.

plus(other)

the associative operation

unit

alias of Just

hymn.types.maybe.from_maybe(self, default)

return the value contained in the Maybe

if the Maybe is Nothing, it returns the default values.

hymn.types.maybe.hyx_XlessHthan_signX_maybe(self, default)

return the value contained in the Maybe

if the Maybe is Nothing, it returns the default values.

hymn.types.maybe.is_nothing(m)

return True if m is Nothing

hymn.types.maybe.maybe(func=None, predicate=None, nothing_on_exceptions=None)

decorator to turn func into monadic function of the Maybe monad

hymn.types.maybe.maybe_m

alias of hymn.types.maybe.Maybe

hymn.types.maybe.unit

alias of hymn.types.maybe.Just

hymn.types.maybe.Nothing = Nothing

the Maybe that represents nothing, a singleton, like None

hymn.types.maybe.zero = Nothing

the Maybe that represents nothing, a singleton, like None

hymn.types.maybe.from_maybe()

alias of from_maybe()

hymn.types.maybe.to_maybe()

alias of from_value()

Hy Specific API

maybe-m

alias of Maybe

Tag Macro

? [f]

turn f into monadic function with maybe()

Functions

<-maybe
from-maybe

alias of Maybe.from_maybe()

->maybe
to-maybe

alias of Maybe.from_value()

nothing?

alias of is_nothing()

Examples

Comparison

Maybes are comparable if the wrapped values are comparable. Just is greater than Nothing in any case.

=> (import [hymn.types.maybe [Just Nothing]])
=> (> (Just 2) (Just 1))
True
=> (= (Just 1) (Just 2))
False
=> (= (Just 2) (Just 2))
True
=> (= Nothing Nothing)
True
=> (= Nothing (Just 1))
False
=> (< (Just -1) Nothing)
False

Do Notation

=> (import [hymn.types.maybe [Just Nothing]])
=> (require [hymn.macros [do-monad-return]])
=> (do-monad-return [a (Just 1) b (Just 2)] (+ a b))
Just(3)
=> (do-monad-return [a (Just 1) b Nothing] (+ a b))
Nothing

Do Notation with :when

=> (import [hymn.types.maybe [maybe-m]])
=> (require [hymn.macros [do-monad-with]])
=> (defn safe-div [a b]
...   (do-monad-with maybe-m [:when (not (zero? b))] (/ a b)))
=> (safe-div 1 2)
Just(0.5)
=> (safe-div 1 0)
Nothing

Operations

Use ->maybe() to create a Maybe from value

=> (import [hymn.types.maybe [->maybe]])
=> (->maybe 42)
Just(42)
=> (->maybe None)
Nothing

nothing?() returns True if the value is Nothing

=> (import [hymn.types.maybe [Just Nothing nothing?]])
=> (nothing? Nothing)
True
=> (nothing? (Just 1))
False

<-maybe() returns the value in the monad, or a default value if it is Nothing

=> (import [hymn.types.maybe [<-maybe ->maybe nothing?]])
=> (nothing? (->maybe None))
True
=> (setv answer (->maybe 42))
=> (setv nothing (->maybe None))
=> (<-maybe answer "not this one")
42
=> (<-maybe nothing "I got nothing")
"I got nothing"

append() adds up the values, handling Nothing gracefully

=> (import [hymn.types.maybe [Just Nothing]])
=> (.append (Just 42) Nothing)
Just(42)
=> (.append (Just 42) (Just 42))
Just(84)
=> (.append Nothing (Just 42))
Just(42)

maybe() turns a function into monadic one

=> (import [hymn.types.maybe [maybe]])
=> (with-decorator maybe (defn add1 [n] (inc (int n))))
=> (add1 "41")
Just(42)
=> (add1 "nan")
Nothing
=> (setv safe-div (maybe /))
=> (safe-div 1 2)
Just(0.5)
=> (safe-div 1 0)
Nothing

Tag Macro

=> (require [hymn.types.maybe [?]])
=> (#? int "42")
Just(42)
=> (#? int "not a number")
Nothing
=> (setv safe-div #? /)
=> (safe-div 1 2)
Just(0.5)
=> (safe-div 1 0)
Nothing