Maybe

=

defn

(λ [(Ref (Maybe a)), (Ref (Maybe a))] Bool)

                    (= a b)
                

equality is defined as the equality of both the constructor and the contained value, i.e. (Just 1) and (Just 1) are the same, but (Just 1) and (Just 2) are not.

Just

template

(λ [a] (Maybe a))

creates a Just.

Nothing

template

(λ [] (Maybe a))

creates a Nothing.

apply

defn

(λ [(Maybe a), (λ [a] b)] (Maybe b))

                    (apply a f)
                

applies a function to the value inside a if it is a Just, and wraps it up again. If it is Nothing, it is just returned.

copy

template

(λ [(Ref (Maybe a))] (Maybe a))

copies a (Maybe a).

delete

template

(λ [(Maybe a)] ())

deletes a (Maybe a). Should usually not be called manually.

from

defn

(λ [(Maybe a), a] a)

                    (from a dflt)
                

is an unwrapper that will get the value from a Just, or a default value if a Nothing is passed.

from-ptr

defn

(λ [(Ptr a)] (Maybe a))

                    (from-ptr a)
                

Creates a (Maybe a) from a (Ptr a). If the Ptr was NULL, this function will return Nothing.

just?

defn

(λ [(Ref (Maybe a))] Bool)

                    (just? a)
                

checks whether the given value a is a Just.

It is the inverse of nothing?.

nothing?

defn

(λ [(Ref (Maybe a))] Bool)

                    (nothing? a)
                

checks whether the given value a is a Nothing.

It is the inverse of just?.

or-zero

defn

(λ [(Maybe a)] a)

                    (or-zero a)
                

is an unwrapper that will get the value from a Just, or build a value using zero if a Nothing is passed.

prn

template

(λ [(Ref (Maybe a))] String)

stringifies a "Maybe".

ptr

defn

(λ [(Ref (Maybe a))] (Ptr a))

                    (ptr a)
                

Creates a (Ptr a) from a (Maybe a). If the Maybe was Nothing, this function will return a NULL value.

str

template

(λ [(Ref (Maybe a))] String)

stringifies a "Maybe".

to-result

defn

(λ [(Maybe a), b] (Result a b))

                    (to-result a error)
                

is a function that will convert a Maybe to a Result, where a Just becomes a Success and a Nothing becomes an Error.

You’ll also need to provide an error message error that is given to the Error constructor if necessary.

unsafe-from

defn

(λ [(Maybe a)] a)

                    (unsafe-from a)
                

is an unsafe unwrapper that will get the value from a Just. If a is Nothing, a runtime error will be generated.