tur/result
ok
(ok [x :int] :ptr<void>)
wrap a value in an ok result.
| x | the success value (:int) |
A heap-allocated ok result as :ptr<void>. (ok 42) ; => ok(42)
err
(err [e :int] :ptr<void>)
wrap a value in an err result.
| e | the error value (:int) |
A heap-allocated err result as :ptr<void>. (err 1) ; => err(1)
ok?
(ok? [r :ptr<void>] :bool)
check whether a result is ok.
| r | the result to test (:ptr<void>) |
true if r is an ok result, false otherwise. (ok? (ok 1)) ; => true (ok? (err 0)) ; => false
err?
(err? [r :ptr<void>] :bool)
check whether a result is err.
| r | the result to test (:ptr<void>) |
true if r is an err result (or NULL), false otherwise. (err? (err 0)) ; => true (err? (ok 1)) ; => false
ok-val
(ok-val [r :ptr<void>] :int)
extract the ok value from a result.
| r | an ok result (:ptr<void>); behaviour is undefined if r is err |
The contained ok value as :int. (ok-val (ok 5)) ; => 5
err-val
(err-val [r :ptr<void>] :int)
extract the err value from a result.
| r | an err result (:ptr<void>); behaviour is undefined if r is ok |
The contained err value as :int. (err-val (err 99)) ; => 99
result-unwrap
(result-unwrap [r :ptr<void>] :int)
extract the ok value, aborting the process if r is err.
| r | the result to unwrap (:ptr<void>) |
The contained ok value as :int. (result-unwrap (ok 7)) ; => 7
result-unwrap-or
(result-unwrap-or [r :ptr<void> default_val :int] :int)
extract the ok value, or return a default if r is err.
| r | the result (:ptr<void>) | |
| default_val | the fallback value (:int) |
The ok value if r is ok, otherwise default_val. (result-unwrap-or (err 0) 99) ; => 99
result-expect
(result-expect [r :ptr<void> msg :cstr] :int)
extract the ok value, aborting with a custom message if r is err.
| r | the result to unwrap (:ptr<void>) | |
| msg | message printed to stderr before aborting (:cstr) |
The contained ok value as :int. (result-expect r "parse failed") ; => ok value or abort
result-map
(result-map [r :ptr<void> f :ptr<void>] :ptr<void>)
apply a function to the ok value, propagating err unchanged.
| r | the result (:ptr<void>) | |
| f | function to apply to the ok value (:ptr<void>); signature fn [x :int] :int |
A new ok result with the mapped value, or r unchanged if r is err. (result-map (ok 3) (fn [x] (* x 2))) ; => ok(6)
result-map-err
(result-map-err [r :ptr<void> f :ptr<void>] :ptr<void>)
apply a function to the err value, propagating ok unchanged.
| r | the result (:ptr<void>) | |
| f | function to apply to the err value (:ptr<void>); signature fn [e :int] :int |
A new err result with the mapped error, or r unchanged if r is ok. (result-map-err (err 1) (fn [e] (+ e 100))) ; => err(101)
result-flat-map
(result-flat-map [r :ptr<void> f :ptr<void>] :ptr<void>)
chain a fallible computation over an ok result.
| r | the result (:ptr<void>) | |
| f | function receiving the ok value and returning a new result (:ptr<void>) |
The result of applying f to the ok value, or r unchanged if r is err. (result-flat-map (ok 5) (fn [x] (if (> x 0) (ok x) (err -1)))) ; => ok(5)
result-or
(result-or [r :ptr<void> alt :ptr<void>] :ptr<void>)
return r if ok, otherwise return the fallback result.
| r | the primary result (:ptr<void>) | |
| alt | the fallback result (:ptr<void>) |
r if r is ok, otherwise alt. (result-or (err 0) (ok 99)) ; => ok(99)
result-or-else
(result-or-else [r :ptr<void> f :ptr<void>] :ptr<void>)
return r if ok, otherwise compute a fallback by calling f with the err value.
| r | the primary result (:ptr<void>) | |
| f | recovery function of type fn [e :int] :ptr<void> |
r if r is ok, otherwise the result of f(err-value). (result-or-else (err 1) (fn [e] (ok 0))) ; => ok(0)
ok-or
(ok-or [opt :ptr<void> e :int] :ptr<void>)
convert an option to a result, using e as the error value if the option is none.
| opt | the option to convert (:ptr<void>) | |
| e | the error value to use if opt is none (:int) |
ok(value) if opt is some(value), err(e) if opt is none. (ok-or (some 7) -1) ; => ok(7) (ok-or (none) -1) ; => err(-1)
err-context
(err-context [r :ptr<void> ctx :cstr] :ptr<void>)
prepend context text to an err value's message string.
| r | the result (:ptr<void>) | |
| ctx | context prefix string (:cstr) |
A new err result whose message is "ctx: original", or r unchanged if r is ok. (err-context (err my-msg) "loading config")
result-eq?
(result-eq? [r1 r2 ok-cmp err-cmp] :bool)
compare two results using separate comparators for ok and err values.
| r1 | first result | |
| r2 | second result | |
| ok-cmp | comparator for ok values; fn [a :int b :int] :bool | |
| err-cmp | comparator for err values; fn [a :int b :int] :bool |
Since: Phase E1
Eq[result]
(definstance Eq [result])
Eq: structural equality via result-eq?.
result-free
(result-free [r :ptr<void>])
free the heap memory for a result value.
| r | the result to free (:ptr<void>) |
(result-free my-result)
result-collect
(result-collect [v :ptr<void>] :ptr<void>)
collect a vec of results into a single result.
| v | a vec (:ptr<void>) whose elements are result ptr<void> values |
ok(vec-of-ok-values) if every element is ok, or err(first-err-value) on the first err encountered. (result-collect my-results-vec) ; => ok(vec) or err(e)
result-partition
(result-partition [v :ptr<void>] :ptr<void>)
split a vec of results into separate ok and err vecs.
| v | a vec (:ptr<void>) whose elements are result ptr<void> values |
A pair (:ptr<void>) with fields ok_vec and err_vec.
Use result-partition-ok and result-partition-err to access each vec.
(let [pair (result-partition my-vec)]
(result-partition-ok pair))
result-partition-ok
(result-partition-ok [pair :ptr<void>] :ptr<void>)
extract the ok-vec from a result-partition pair.
| pair | the pair returned by result-partition (:ptr<void>) |
The vec (:ptr<void>) of ok values. (result-partition-ok (result-partition my-vec))
result-partition-err
(result-partition-err [pair :ptr<void>] :ptr<void>)
extract the err-vec from a result-partition pair.
| pair | the pair returned by result-partition (:ptr<void>) |
The vec (:ptr<void>) of err values. (result-partition-err (result-partition my-vec))
result-must
(result-must [r :ptr<void>] :int)
unwrap the ok value or panic with a default message.
| r | the result to unwrap (:ptr<void>) |
Since: Phase R4
result-must-msg
(result-must-msg [r :ptr<void> msg :cstr] :int)
unwrap the ok value or panic with a caller-supplied message.
| r | the result to unwrap (:ptr<void>) | |
| msg | the panic message (:cstr) |
Since: Phase R4
Foldable[ptr<void>]
(definstance Foldable [ptr<void>])
Foldable: foldl and foldr over the ok value.
Bifunctor[ptr<void>]
(definstance Bifunctor [ptr<void>])
Bifunctor: bimap over ok and err values.
Functor[ptr<void>]
(definstance Functor [ptr<void>])
Functor: fmap over the ok value.
Applicative[ptr<void>]
(definstance Applicative [ptr<void>])
Applicative: pure and ap.
Monad[ptr<void>]
(definstance Monad [ptr<void>])
Monad: bind (flat-map over the ok value).
Internal definitions
__foldable_result_foldl-- SINCE: Phase HKT H3__foldable_result_foldr-- foldr implementation for the result Foldable instance.__bifunctor_result_bimap-- bimap implementation for the result Bifunctor instance.__functor_result_fmap-- fmap implementation for the result Functor instance.__applicative_result_pure-- pure implementation for the result Applicative instance.__applicative_result_ap-- ap implementation for the result Applicative instance.__monad_result_bind-- bind implementation for the result Monad instance.