tur/result

stdlib/result.tur
defn

ok

(ok [x :int] :ptr<void>)

wrap a value in an ok result.

xthe success value (:int)
A heap-allocated ok result as :ptr<void>.

  (ok 42)  ; => ok(42)
defn

err

(err [e :int] :ptr<void>)

wrap a value in an err result.

ethe error value (:int)
A heap-allocated err result as :ptr<void>.

  (err 1)  ; => err(1)
defn

ok?

(ok? [r :ptr<void>] :bool)

check whether a result is ok.

rthe result to test (:ptr<void>)
true if r is an ok result, false otherwise.

  (ok? (ok 1))   ; => true
  (ok? (err 0))  ; => false
defn

err?

(err? [r :ptr<void>] :bool)

check whether a result is err.

rthe result to test (:ptr<void>)
true if r is an err result (or NULL), false otherwise.

  (err? (err 0))  ; => true
  (err? (ok 1))   ; => false
defn

ok-val

(ok-val [r :ptr<void>] :int)

extract the ok value from a result.

ran ok result (:ptr<void>); behaviour is undefined if r is err
The contained ok value as :int.

  (ok-val (ok 5))  ; => 5
defn

err-val

(err-val [r :ptr<void>] :int)

extract the err value from a result.

ran err result (:ptr<void>); behaviour is undefined if r is ok
The contained err value as :int.

  (err-val (err 99))  ; => 99
defn

result-unwrap

(result-unwrap [r :ptr<void>] :int)

extract the ok value, aborting the process if r is err.

rthe result to unwrap (:ptr<void>)
The contained ok value as :int.

  (result-unwrap (ok 7))  ; => 7
defn

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.

rthe result (:ptr<void>)
default_valthe fallback value (:int)
The ok value if r is ok, otherwise default_val.

  (result-unwrap-or (err 0) 99)  ; => 99
defn

result-expect

(result-expect [r :ptr<void> msg :cstr] :int)

extract the ok value, aborting with a custom message if r is err.

rthe result to unwrap (:ptr<void>)
msgmessage printed to stderr before aborting (:cstr)
The contained ok value as :int.

  (result-expect r "parse failed")  ; => ok value or abort
defn

result-map

(result-map [r :ptr<void> f :ptr<void>] :ptr<void>)

apply a function to the ok value, propagating err unchanged.

rthe result (:ptr<void>)
ffunction 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)
defn

result-map-err

(result-map-err [r :ptr<void> f :ptr<void>] :ptr<void>)

apply a function to the err value, propagating ok unchanged.

rthe result (:ptr<void>)
ffunction 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)
defn

result-flat-map

(result-flat-map [r :ptr<void> f :ptr<void>] :ptr<void>)

chain a fallible computation over an ok result.

rthe result (:ptr<void>)
ffunction 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)
defn

result-or

(result-or [r :ptr<void> alt :ptr<void>] :ptr<void>)

return r if ok, otherwise return the fallback result.

rthe primary result (:ptr<void>)
altthe fallback result (:ptr<void>)
r if r is ok, otherwise alt.

  (result-or (err 0) (ok 99))  ; => ok(99)
defn

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.

rthe primary result (:ptr<void>)
frecovery 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)
defn

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.

optthe option to convert (:ptr<void>)
ethe 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)
defn

err-context

(err-context [r :ptr<void> ctx :cstr] :ptr<void>)

prepend context text to an err value's message string.

rthe result (:ptr<void>)
ctxcontext 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")
defn

result-eq?

(result-eq? [r1 r2 ok-cmp err-cmp] :bool)

compare two results using separate comparators for ok and err values.

r1first result
r2second result
ok-cmpcomparator for ok values; fn [a :int b :int] :bool
err-cmpcomparator for err values; fn [a :int b :int] :bool

Since: Phase E1

definstance

Eq[result]

(definstance Eq [result])

Eq: structural equality via result-eq?.

defn

result-free

(result-free [r :ptr<void>])

free the heap memory for a result value.

rthe result to free (:ptr<void>)
(result-free my-result)
defn

result-collect

(result-collect [v :ptr<void>] :ptr<void>)

collect a vec of results into a single result.

va 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)
defn

result-partition

(result-partition [v :ptr<void>] :ptr<void>)

split a vec of results into separate ok and err vecs.

va 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))
defn

result-partition-ok

(result-partition-ok [pair :ptr<void>] :ptr<void>)

extract the ok-vec from a result-partition pair.

pairthe pair returned by result-partition (:ptr<void>)
The vec (:ptr<void>) of ok values.

  (result-partition-ok (result-partition my-vec))
defn

result-partition-err

(result-partition-err [pair :ptr<void>] :ptr<void>)

extract the err-vec from a result-partition pair.

pairthe pair returned by result-partition (:ptr<void>)
The vec (:ptr<void>) of err values.

  (result-partition-err (result-partition my-vec))
defn

result-must

(result-must [r :ptr<void>] :int)

unwrap the ok value or panic with a default message.

rthe result to unwrap (:ptr<void>)

Since: Phase R4

defn

result-must-msg

(result-must-msg [r :ptr<void> msg :cstr] :int)

unwrap the ok value or panic with a caller-supplied message.

rthe result to unwrap (:ptr<void>)
msgthe panic message (:cstr)

Since: Phase R4

definstance

Foldable[ptr<void>]

(definstance Foldable [ptr<void>])

Foldable: foldl and foldr over the ok value.

definstance

Bifunctor[ptr<void>]

(definstance Bifunctor [ptr<void>])

Bifunctor: bimap over ok and err values.

definstance

Functor[ptr<void>]

(definstance Functor [ptr<void>])

Functor: fmap over the ok value.

definstance

Applicative[ptr<void>]

(definstance Applicative [ptr<void>])

Applicative: pure and ap.

definstance

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.