Result
Definitions
def
ap
[etefu]
(
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
[tefe]
(
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
[ret]
(
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
[tefe]
(
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
[te]
(
f :
t -> Bool
r :
Result[e, t]
)
: Option[t]
\ Pure
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
[t1efet2]
(
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
[et]
(
r :
Result[e, Result[e, t]]
)
: Result[e, t]
\ Pure
Returns v
if r
is Ok(v)
. Returns Err(w)
if r
is Err(w)
.
def
foldLeft
[atefe]
(
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
[baefe]
(
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
[taefe]
(
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
[abefe]
(
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
[tefae]
(
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
[tefe]
(
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
[tefe]
(
f :
t -> Unit \ ef
r :
Result[e, t]
)
: Unit
\ ef
Applies f
to v
if r
is Ok(v)
. Otherwise does nothing.
def
getWithDefault
[te]
(
d :
t
r :
Result[e, t]
)
: t
\ Pure
Returns v
if r
is Ok(v)
. Otherwise returns d
.
def
isErr
[et]
(
r :
Result[e, t]
)
: Bool
\ Pure
Returns true
iff r
is Err(w)
.
def
isOk
[et]
(
r :
Result[e, t]
)
: Bool
\ Pure
Returns true
iff r
is Ok(v)
.
def
iterator
[ret]
(
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
[t1eft2e]
(
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
[t1t2t3t4t5t6t7t8t9t10efue]
(
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
[t1t2efue]
(
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
[t1t2t3efue]
(
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
[t1t2t3t4efue]
(
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
[t1t2t3t4t5efue]
(
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
[t1t2t3t4t5t6efue]
(
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
[t1t2t3t4t5t6t7efue]
(
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
[t1t2t3t4t5t6t7t8efue]
(
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
[t1t2t3t4t5t6t7t8t9efue]
(
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
[e1efe2t]
(
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
[te]
(
from :
{ from = t }
to :
{ to = t }
r :
Result[e, t]
)
: Result[e, t]
\ Pure
with
Eq[t]
Returns Ok(to)
if r
is Ok(from)
. Otherwise returns r
.
def
sequence
[ea]
(
l :
List[Result[e, a]]
)
: Result[e, List[a]]
\ Pure
Returns Ok(v1 :: v2 :: ... :: vn)
if each of l_i
is Ok(v_i)
.
Otherwise returns the first Err
encountered.
def
sum
[e]
(
r :
Result[e, Int32]
)
: Int32
\ Pure
Returns v
if r
is Ok(v)
else 0
.
def
sumWith
[tefe]
(
f :
t -> Int32 \ ef
r :
Result[e, t]
)
: Int32
\ ef
Returns f(v)
if r
is Ok(v)
else 0
.
def
toList
[et]
(
r :
Result[e, t]
)
: List[t]
\ Pure
Returns a one-element list of the value v
if r
is Ok(v)
. Otherwise returns the empty list.
def
toMap
[ekv]
(
r :
Result[e, (k, v)]
)
: Map[k, v]
\ Pure
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
[ab]
(
f :
a -> b
s :
Result[a, a]
)
: Map[a, b]
\ Pure
with
Order[a]
Returns a map with elements of s
as keys and f
applied as values.
def
toOption
[et]
(
r :
Result[e, t]
)
: Option[t]
\ Pure
Returns Some(v)
if r
is Ok(v)
. Otherwise returns None
.
def
toSet
[et]
(
r :
Result[e, t]
)
: Set[t]
\ Pure
with
Order[t]
Returns a one-element set of the value v
if r
is Ok(v)
. Otherwise returns the empty set.
def
toValidation
[et]
(
r :
Result[e, t]
)
: Validation[e, t]
\ Pure
Returns a Success(v)
if r
is Ok(v)
. Otherwise returns a
Failure(Nec.singleton(t))
if r
is Err(t)
.
def
traverse
[aefeb]
(
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
[aefeb]
(
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
[efa]
(
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
[e2te1]
(
default :
{ default = Result[e2, t] }
r :
Result[e1, t]
)
: Result[e2, t]
\ Pure
Returns Ok(v)
if r
is Ok(v)
. Otherwise returns default
.