# Result

`@MustUse `
`enum Result[e: Type, t: Type] with Eq, Order, ToString, Sendable`Source
`case Ok(t)``case Err(e)`

The Result type.

A result represents a successful value or an error value. The constructor `Ok(v)` represents the successful value `v`, whereas the constructor `Err(v)` represents the error value `v`.

### Instances

`instance Applicative[Result[e]]`Source
`instance Eq[Result[e, t]] with Eq[e], Eq[t]`Source
`instance Functor[Result[e]]`Source
`instance Hash[Result[e, t]] with Hash[e], Hash[t]`Source
`instance Monad[Result[e]]`Source
`instance Order[Result[e, t]] with Order[e], Order[t]`Source
`instance Sendable[Result[e, t]] with Sendable[e], Sendable[t]`Source
`instance ToString[Result[e, t]] with ToString[e], ToString[t]`Source

## Definitions

`def ap(r1: Result[e, t -> u \ ef], r2: Result[e, t]): Result[e, u] \ ef`

Applies the function in `r1` to the value in `r2`.

`def count(f: t -> Bool \ ef, r: Result[e, t]): Int32 \ ef`

Returns `1` if `r` is `Ok(v)` and the predicate `f(v)` evaluates to `true`. Otherwise returns `0`.

`def enumerator(rc: Region[r], r: Result[e, t]): Iterator[(Int32, t), r, r] \ r`

Returns an iterator over `r` zipped with the indices of the elements.

`def exists(f: t -> Bool \ ef, r: Result[e, t]): Bool \ ef`

Returns `true` if `r` is `Ok(v)` and the predicate `f(v)` evaluates to `true`. Otherwise returns `false`.

`def find(f: t -> Bool, r: Result[e, t]): Option[t]`

Returns `Some(v)` if `r` is `Ok(v)` and the predicate `f(v)` evaluates to `true`. Otherwise returns `None`.

The function `f` must be pure.

`def flatMap(f: t1 -> Result[e, t2] \ ef, r: Result[e, t1]): Result[e, t2] \ ef`

Returns `f(v)` if `r` is `Ok(v)`. Returns `Err(w)` if `r` is `Err(w)`.

`def flatten(r: Result[e, Result[e, t]]): Result[e, t]`

Returns `v` if `r` is `Ok(v)`. Returns `Err(w)` if `r` is `Err(w)`.

`def foldLeft(f: a -> (t -> a \ ef), z: a, r: Result[e, t]): a \ ef`

Returns `f(z, v)` if `r` is `Ok(v)`. Otherwise returns `z`.

`def foldLeftM(f: b -> (a -> Result[e, b] \ ef), s: b, l: List[a]): Result[e, b] \ ef`

Returns the result of applying `f` to a start value `s` and the elements in `l` going from left to right.

If at any step applying `f` fails (i.e. it produces a `Err(e)` value) the traversal of `l` is short-circuited and `Err(e)` is returned.

If `f` is successfully applied to all elements in `l` the result is of the form: `Ok(f(...f(f(s, x1), x2)..., xn))`.

`def foldRight(f: t -> (a -> a \ ef), z: a, r: Result[e, t]): a \ ef`

Returns `f(v, z)` if `r` is `Ok(v)`. Otherwise returns `z`.

`def foldRightM(f: a -> (b -> Result[e, b] \ ef), s: b, l: List[a]): Result[e, b] \ ef`

Returns the result of applying `f` to a start value `s` and the elements in `l` going from right to left.

If at any step applying `f` fails (i.e. it produces a `Err(e)` value) the traversal of `l` is short-circuited and `Err(e)` is returned.

If `f` is successfully applied to all elements in `l` the result is of the form: `Ok(f(x1, ...f(xn-1, f(xn, s))...))`.

`def foldRightWithCont(f: t -> ((Unit -> a \ ef) -> a \ ef), z: a, r: Result[e, t]): a \ ef`

Returns `f(v, z)` if `r` is `Ok(v)`. Otherwise returns `z`.

A `foldRightWithCont` allows early termination by not calling the continuation.

`def forAll(f: t -> Bool \ ef, r: Result[e, t]): Bool \ ef`

Returns `true` if `r` is `Ok(v)` and the predicate `f(v)` evaluates to `true` or if `r` is `Err(w)`. Otherwise returns `false`.

`def forEach(f: t -> Unit \ ef, r: Result[e, t]): Unit \ ef`

Applies `f` to `v` if `r` is `Ok(v)`. Otherwise does nothing.

`def getWithDefault(d: t, r: Result[e, t]): t`

Returns `v` if `r` is `Ok(v)`. Otherwise returns `d`.

`def isErr(r: Result[e, t]): Bool`

Returns `true` iff `r` is `Err(w)`.

`def isOk(r: Result[e, t]): Bool`

Returns `true` iff `r` is `Ok(v)`.

`def iterator(rc: Region[r], r: Result[e, t]): Iterator[t, r, r] \ r`

Returns an iterator over `r` with 1 element or an empty iterator if `r` is `Err`.

`def map(f: t1 -> t2 \ ef, r: Result[e, t1]): Result[e, t2] \ ef`

Returns `Ok(f(v))` if `r` is `Ok(v)`. Returns `Err(w)` if `r` is `Err(w)`.

`def map10(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> (t8 -> (t9 -> (t10 -> u \ ef))))))))), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3], r4: Result[e, t4], r5: Result[e, t5], r6: Result[e, t6], r7: Result[e, t7], r8: Result[e, t8], r9: Result[e, t9], r10: Result[e, t10]): Result[e, u] \ ef`

Applies the 10-ary function `f` to the values in `r1`, `r2`, ... `r10`.

Returns the first `Err(e)` value if any of `r1`, `r2`, ... `r10` are `Err(e)`.

`def map2(f: t1 -> (t2 -> u \ ef), r1: Result[e, t1], r2: Result[e, t2]): Result[e, u] \ ef`

Applies the binary function `f` to the values in `r1` and `r2`.

Returns the first `Err(e)` value if either of `r1` and `r2` are `Err(e)`.

`def map3(f: t1 -> (t2 -> (t3 -> u \ ef)), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3]): Result[e, u] \ ef`

Applies the ternary function `f` to the values in `r1`, `r2` and `r3`.

Returns the first `Err(e)` value if any of `r1`, `r2` and `r3` are `Err(e)`.

`def map4(f: t1 -> (t2 -> (t3 -> (t4 -> u \ ef))), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3], r4: Result[e, t4]): Result[e, u] \ ef`

Applies the 4-ary function `f` to the values in `r1`, `r2`, `r3` and `r4`.

Returns the first `Err(e)` value if any of `r1`, `r2`, `r3` and `r4` are `Err(e)`.

`def map5(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> u \ ef)))), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3], r4: Result[e, t4], r5: Result[e, t5]): Result[e, u] \ ef`

Applies the 5-ary function `f` to the values in `r1`, `r2`, ... `r5`.

Returns the first `Err(e)` value if any of `r1`, `r2`, ... `r5` are `Err(e)`.

`def map6(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> u \ ef))))), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3], r4: Result[e, t4], r5: Result[e, t5], r6: Result[e, t6]): Result[e, u] \ ef`

Applies the 6-ary function `f` to the values in `r1`, `r2`, ... `r6`.

Returns the first `Err(e)` value if any of `r1`, `r2`, ... `r6` are `Err(e)`.

`def map7(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> u \ ef)))))), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3], r4: Result[e, t4], r5: Result[e, t5], r6: Result[e, t6], r7: Result[e, t7]): Result[e, u] \ ef`

Applies the 7-ary function `f` to the values in `r1`, `r2`, ... `r7`.

Returns the first `Err(e)` value if any of `r1`, `r2`, ... `r7` are `Err(e)`.

`def map8(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> (t8 -> u \ ef))))))), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3], r4: Result[e, t4], r5: Result[e, t5], r6: Result[e, t6], r7: Result[e, t7], r8: Result[e, t8]): Result[e, u] \ ef`

Applies the 8-ary function `f` to the values in `r1`, `r2`, ... `r8`.

Returns the first `Err(e)` value if any of `r1`, `r2`, ... `r8` are `Err(e)`.

`def map9(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> (t8 -> (t9 -> u \ ef)))))))), r1: Result[e, t1], r2: Result[e, t2], r3: Result[e, t3], r4: Result[e, t4], r5: Result[e, t5], r6: Result[e, t6], r7: Result[e, t7], r8: Result[e, t8], r9: Result[e, t9]): Result[e, u] \ ef`

Applies the 9-ary function `f` to the values in `r1`, `r2`, ... `r9`.

Returns the first `Err(e)` value if any of `r1`, `r2`, ... `r9` are `Err(e)`.

`def mapErr(f: e1 -> e2 \ ef, r: Result[e1, t]): Result[e2, t] \ ef`

Returns `Err(f(e))` if `r` is `Err(e)`. Returns `Ok(v)` if `r` is `Ok(v)`.

`def replace(src: { src = t }, dst: { dst = t }, r: Result[e, t]): Result[e, t] with Eq[t]`

Returns `Ok(dst)` if `r` is `Ok(src)`. Otherwise returns `r`.

`def sequence(l: List[Result[e, a]]): Result[e, List[a]]`

Returns `Ok(v1 :: v2 :: ... :: vn)` if each of `l_i` is `Ok(v_i)`. Otherwise returns the first `Err` encountered.

`def sum(r: Result[e, Int32]): Int32`

Returns `v` if `r` is `Ok(v)` else `0`.

`def sumWith(f: t -> Int32 \ ef, r: Result[e, t]): Int32 \ ef`

Returns `f(v)` if `r` is `Ok(v)` else `0`.

`def toList(r: Result[e, t]): List[t]`

Returns a one-element list of the value `v` if `r` is `Ok(v)`. Otherwise returns the empty list.

`def toMap(r: Result[e, (k, v)]): Map[k, v] with Order[k]`

Returns a singleton map with the mapping `k -> v` if `o` is `Ok((k, v))`. Otherwise returns the empty map.

`def toMapWith(f: a -> b, s: Result[a, a]): Map[a, b] with Order[a]`

Returns a map with elements of `s` as keys and `f` applied as values.

`def toOption(r: Result[e, t]): Option[t]`

Returns `Some(v)` if `r` is `Ok(v)`. Otherwise returns `None`.

`def toSet(r: Result[e, t]): Set[t] with Order[t]`

Returns a one-element set of the value `v` if `r` is `Ok(v)`. Otherwise returns the empty set.

`def toValidation(r: Result[e, t]): Validation[e, t]`

Returns a `Success(v)` if `r` is `Ok(v)`. Otherwise returns a `Failure(Nec.singleton(t))` if `r` is `Err(t)`.

`def traverse(f: a -> Result[e, b] \ ef, l: List[a]): Result[e, List[b]] \ ef`

Returns `Some(v1 :: v2 :: ... v :: vn)` if each of `f(l_i)` is `Ok(v_i)`. Otherwise returns the first `Err` encountered.

`def traverseX(f: a -> Result[e, b] \ ef, l: List[a]): Result[e, Unit] \ ef`

Returns `Ok()` if each of `f(l_i)` is `Ok(_)`. Otherwise returns `None`.

This function is the "forgetful" version of `traverse`, use it when the you want the effect of applying `f` to each element but do not care about collecting the results.

`def tryCatch(f: Unit -> a \ ef): Result[String, a] \ ef`

Returns `Ok(x)` if `f` was invoked without throwing an exception.

If `f` throws a Java `Exception`, `Err(e)` is returned where `e` is the error message.

`def withDefault(default: { default = Result[e2, t] }, r: Result[e1, t]): Result[e2, t]`

Returns `Ok(v)` if `r` is `Ok(v)`. Otherwise returns `default`.