Maybe
is a data type to represent optional values. It has two
constructors, (Just <value>) and Nothing, and provides many functions to
wrap and unwrap values.
=
(Fn [(Ref (Maybe a) b), (Ref (Maybe a) b)] 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.
apply
(Fn [(Maybe a), (Ref (Fn [a] b c) d)] (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.
from
(Fn [(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
(Fn [(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?
(Fn [(Ref (Maybe a) b)] Bool)
(just? a)
checks whether the given value a is a Just.
It is the inverse of nothing?.
nothing?
(Fn [(Ref (Maybe a) b)] Bool)
(nothing? a)
checks whether the given value a is a Nothing.
It is the inverse of just?.
or-zero
(Fn [(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.
to-result
(Fn [(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
(Fn [(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.
unsafe-ptr
(Fn [(Ref (Maybe a) b)] (Ptr a))
(unsafe-ptr a)
Creates a (Ptr a) from a (Maybe a). If the Maybe was
Nothing, this function will return a NULL value.