Result

is a data type for computations that might fail with an error. It has two constructors, (Success <value>) and (Error <value>), and provides many functions for working with, combining, and wrapping and unwrapping values.

=

defn

(Fn [(Ref (Result a b) c), (Ref (Result a b) c)] Bool)

                        (= a b)
                    

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

Error

template

(Fn [a] (Result b a))

creates a Error.

Success

template

(Fn [a] (Result a b))

creates a Success.

and-then

defn

(Fn [(Result a b), (Ref (Fn [a] (Result c b) d) e)] (Result c b))

                        (and-then a f)
                    

takes a Result and applies a function f to it if it is a Success type. In the case that it is an Error, it is returned as is.

It is thus quite similar to map, but it will unwrap the value.

apply

defn

(Fn [(Result a b), (Ref (Fn [a] c d) e), (Ref (Fn [b] f d) g)] (Result c f))

                        (apply a success-f error-f)
                    

takes a Result a and applies functions to them, one in the case that it is an Error, one in the case that it is a Success.

copy

template

(Fn [(Ref (Result a b) c)] (Result a b))

copies a Result.

delete

template

(Fn [(Result a b)] ())

deletes a Result. This should usually not be called manually.

error?

defn

(Fn [(Ref (Result a b) c)] Bool)

                        (error? a)
                    

checks whether the given value a is an Error.

It is the inverse of success?.

from-error

defn

(Fn [(Result a b), b] b)

                        (from-error a dflt)
                    

is an unwrapper that will get the value from an Error. If a is a Success, a default value will be returned.

from-success

defn

(Fn [(Result a b), a] a)

                        (from-success a dflt)
                    

is an unwrapper that will get the value from a Success. If a is an Error, a default value will be returned.

get-tag

instantiate

(Fn [(Ref (Result a b) c)] Int)

Gets the tag from a Result.

map

defn

(Fn [(Result a b), (Ref (Fn [a] c d) e)] (Result c b))

                        (map a f)
                    

takes a Result and applies a function f to it if it is a Success type, and wraps it back up. In the case that it is an Error, it is returned as is.

map-error

defn

(Fn [(Result a b), (Ref (Fn [b] c d) e)] (Result a c))

                        (map-error a f)
                    

takes a Result and applies a function f to it if it is an Error type, and wraps it back up. In the case that it is a Success, it is returned as is.

or-else

defn

(Fn [(Result a b), (Ref (Fn [b] (Result a c) d) e)] (Result a c))

                        (or-else a f)
                    

takes a Result and applies a function f to it if it is an Error type. In the case that it is a Success, it is returned as is.

It is the inverse of and-then.

prn

template

(Fn [(Ref (Result a b) c)] String)

converts a Result to a string.

str

template

(Fn [(Ref (Result a b) c)] String)

converts a Result to a string.

success?

defn

(Fn [(Ref (Result a b) c)] Bool)

                        (success? a)
                    

checks whether the given value a is a Success.

It is the inverse of error?.

to-maybe

defn

(Fn [(Result a b)] (Maybe a))

                        (to-maybe a)
                    

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

The error value is thrown away.

unsafe-from-error

defn

(Fn [(Result a b)] b)

                        (unsafe-from-error a)
                    

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

unsafe-from-success

defn

(Fn [(Result a b)] a)

                        (unsafe-from-success a)
                    

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

unwrap-or-else

defn

(Fn [(Result a b), (Ref (Fn [b] a c) d)] a)

                        (unwrap-or-else a f)
                    

takes a Result and either unwraps it if it is a Success, or calls a function f on the value contained in the Error.

unwrap-or-zero

defn

(Fn [(Result a b)] a)

                        (unwrap-or-zero a)
                    

takes a Result and either unwraps it if it is a Success, or calls zero. zero must be defined on the Success type.