Option
case None
case Some(t)
The Option type.
An option represents an optional value.
The constructor None
represents an absent value,
whereas the constructor Some(v)
represents the value v
.
Instances
instance Applicative[Option]
Sourceinstance CommutativeMonoid[Option[a]] with CommutativeMonoid[a]
Sourceinstance CommutativeSemiGroup[Option[a]] with CommutativeSemiGroup[a]
Sourceinstance Filterable[Option]
Sourceinstance JoinLattice[Option[t]] with JoinLattice[t]
Sourceinstance LowerBound[Option[t]]
Sourceinstance MeetLattice[Option[t]] with MeetLattice[t]
Sourceinstance PartialOrder[Option[t]] with PartialOrder[t]
Sourceinstance Traversable[Option]
Sourceinstance UnorderedFoldable[Option]
Sourceinstance Witherable[Option]
SourceDefinitions
def ap(f: Option[a -> b \ ef], x: Option[a]): Option[b] \ ef
SourceIf both arguments are Some
, return a Some
containing the result of applying the function inside
f
to the value inside x
. Otherwise return None
.
def count(f: a -> Bool \ ef, o: Option[a]): Int32 \ ef
SourceReturns 1
if o
is Some(v)
and the predicate f(v)
evaluates to true
. Otherwise returns 0
.
def empty(): Option[a]
SourceReturns the empty option variant None
.
def enumerator(rc: Region[r], o: Option[a]): Iterator[(Int32, a), r, r] \ r
SourceReturns an iterator over o
zipped with the indices of the elements.
def exists(f: a -> Bool \ ef, o: Option[a]): Bool \ ef
SourceReturns true
if o
is Some(v)
and the predicate f(v)
evaluates to true
. Otherwise returns false
.
def filter(f: a -> Bool \ ef, o: Option[a]): Option[a] \ ef
SourceReturns o
if o
is Some(v)
and the predicate f(v)
is true. Otherwise returns None
.
def find(f: a -> Bool, o: Option[a]): Option[a]
SourceReturns o
if o
is Some(v)
and the predicate f(v)
evaluates to true
. Otherwise returns None
.
The function f
must be pure.
def flatMap(f: a -> Option[b] \ ef, o: Option[a]): Option[b] \ ef
SourceReturns f(v)
if o
is Some(v)
. Otherwise returns None
.
def flatten(o: Option[Option[a]]): Option[a]
SourceReturns v
if o
is Some(v)
. Otherwise returns None
.
Returns the result of applying combine
to all the elements in o
, using empty
as the initial value.
def foldLeft(f: b -> (a -> b \ ef), z: b, o: Option[a]): b \ ef
SourceReturns f(z, v)
if o
is Some(v)
. Otherwise returns z
.
def foldLeftM(f: b -> (a -> Option[b] \ ef), s: b, l: List[a]): Option[b] \ ef
SourceReturns 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 None
value) the traversal
of l
is short-circuited and None
is returned.
If f
is successfully applied to all the elements in l
the result is of the form:
Some(f(...f(f(s, x1), x2)..., xn))
.
Returns the result of mapping each element and combining the results.
def foldRight(f: a -> (b -> b \ ef), o: Option[a], z: b): b \ ef
SourceReturns f(v, z)
if o
is Some(v)
. Otherwise returns z
.
def foldRightM(f: a -> (b -> Option[b] \ ef), s: b, l: List[a]): Option[b] \ ef
SourceReturns 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 None
value) the traversal
of l
is short-circuited and None
is returned.
If f
is successfully applied to al elements in l
the result is of the form:
Some(f(x1, ...f(xn-1, f(xn, s))...))
.
def foldRightWithCont(f: a -> ((Unit -> b \ ef) -> b \ ef), o: Option[a], z: b): b \ ef
SourceReturns f(v, z)
if o
is Some(v)
. Otherwise returns z
.
A foldRightWithCont
allows early termination by not calling the continuation.
def forAll(f: a -> Bool \ ef, o: Option[a]): Bool \ ef
SourceReturns true
if o
is Some(v)
and the predicate f(v)
evaluates to true
or if o
is None
.
Otherwise returns false
.
def forEach(f: a -> Unit \ ef, o: Option[a]): Unit \ ef
SourceApplies f
to v
if o
is Some(v)
. Otherwise does nothing.
def getWithDefault(d: a, o: Option[a]): a
SourceReturns v
if o
is Some(v).
Otherwise returns d
.
def isEmpty(o: Option[a]): Bool
SourceReturns true
iff o
is None
.
def iterator(rc: Region[r], o: Option[a]): Iterator[a, r, r] \ r
SourceReturns an iterator over o
with 1 element or an empty iterator if o
is None
.
def map(f: a -> b \ ef, o: Option[a]): Option[b] \ ef
SourceReturns Some(f(v))
if o
is Some(v)
. Otherwise returns None
.
def map10(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> (t8 -> (t9 -> (t10 -> u \ ef))))))))), o1: Option[t1], o2: Option[t2], o3: Option[t3], o4: Option[t4], o5: Option[t5], o6: Option[t6], o7: Option[t7], o8: Option[t8], o9: Option[t9], o10: Option[t10]): Option[u] \ ef
SourceApplies the 10-ary function f
to the values in o1
, o2
, ... o10
.
Returns None
if any of o1
, o2
, ... o10
are None
.
def map2(f: t1 -> (t2 -> u \ ef), o1: Option[t1], o2: Option[t2]): Option[u] \ ef
SourceApplies the binary function f
to the values in o1
and o2
.
Returns None
if either o1
or o2
are None
.
def map3(f: t1 -> (t2 -> (t3 -> u \ ef)), o1: Option[t1], o2: Option[t2], o3: Option[t3]): Option[u] \ ef
SourceApplies the ternary function f
to the values in o1
, o2
and o3
.
Returns None
if any of o1
, o2
and o3
are None
.
def map4(f: t1 -> (t2 -> (t3 -> (t4 -> u \ ef))), o1: Option[t1], o2: Option[t2], o3: Option[t3], o4: Option[t4]): Option[u] \ ef
SourceApplies the 4-ary function f
to the values in o1
, o2
, o3
and o4
.
Returns None
if any of o1
, o2
, o3
and o4
are None
.
def map5(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> u \ ef)))), o1: Option[t1], o2: Option[t2], o3: Option[t3], o4: Option[t4], o5: Option[t5]): Option[u] \ ef
SourceApplies the 5-ary function f
to the values in o1
, o2
, ... o5
.
Returns None
if any of o1
, o2
, ... o5
are None
.
def map6(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> u \ ef))))), o1: Option[t1], o2: Option[t2], o3: Option[t3], o4: Option[t4], o5: Option[t5], o6: Option[t6]): Option[u] \ ef
SourceApplies the 6-ary function f
to the values in o1
, o2
, ... o6
.
Returns None
if any of o1
, o2
, ... o6
are None
.
def map7(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> u \ ef)))))), o1: Option[t1], o2: Option[t2], o3: Option[t3], o4: Option[t4], o5: Option[t5], o6: Option[t6], o7: Option[t7]): Option[u] \ ef
SourceApplies the 7-ary function f
to the values in o1
, o2
, ... o7
.
Returns None
if any of o1
, o2
, ... o7
are None
.
def map8(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> (t8 -> u \ ef))))))), o1: Option[t1], o2: Option[t2], o3: Option[t3], o4: Option[t4], o5: Option[t5], o6: Option[t6], o7: Option[t7], o8: Option[t8]): Option[u] \ ef
SourceApplies the 8-ary function f
to the values in o1
, o2
, ... o8
.
Returns None
if any of o1
, o2
, ... o8
are None
.
def map9(f: t1 -> (t2 -> (t3 -> (t4 -> (t5 -> (t6 -> (t7 -> (t8 -> (t9 -> u \ ef)))))))), o1: Option[t1], o2: Option[t2], o3: Option[t3], o4: Option[t4], o5: Option[t5], o6: Option[t6], o7: Option[t7], o8: Option[t8], o9: Option[t9]): Option[u] \ ef
SourceApplies the 9-ary function f
to the values in o1
, o2
, ... o9
.
Returns None
if any of o1
, o2
, ... o9
are None
.
def nonEmpty(o: Option[a]): Bool
SourceReturns true
iff o
is Some
.
def point(x: a): Option[a]
SourceReturns Some(x)
.
Returns Some(dst)
if o
is Some(src)
. Otherwise returns o
.
def sequence(l: List[Option[a]]): Option[List[a]]
SourceReturns Some(v1 :: v2 :: ... :: vn)
if each of xs_i
is Some(v_i)
. Otherwise returns None
.
def sum(o: Option[Int32]): Int32
SourceReturns v
if o
is Some(v)
else 0
.
def sumWith(f: a -> Int32 \ ef, o: Option[a]): Int32 \ ef
SourceReturns f(v)
if o
is Some(v)
else 0
.
def toErr(d: t, o: Option[e]): Result[e, t]
SourceReturns the Option value Err(e)
if o
is Some(e)
. Otherwise returns Ok(d)
.
def toFailure(d: t, o: Option[e]): Validation[e, t]
SourceReturns e
into Validation's Failure
if o
is Some(e)
. Otherwise returns Success(d)
.
def toList(o: Option[a]): List[a]
SourceReturns a one-element list of the value v
if o
is Some(v)
. Otherwise returns the empty list.
Returns a singleton map with the mapping k -> v
if o
is Some((k, v))
. Otherwise returns the empty map.
Returns a map with elements of s
as keys and f
applied as values.
def toOk(e: e, o: Option[t]): Result[e, t]
SourceReturns the Option value Ok(v)
if o
is Some(v)
. Otherwise returns Err(e)
.
Returns a one-element set of the value v
if o
is Some(v)
. Otherwise returns the empty set.
def toSuccess(e: e, o: Option[t]): Validation[e, t]
SourceReturns the Validation value Success(v)
if o
is Some(v)
. Otherwise lifts e
into Validation's Failure
.
def traverse(f: a -> Option[b] \ ef, l: List[a]): Option[List[b]] \ ef
SourceReturns Some(v1 :: v2 :: ... v :: vn)
if each of f(l_i)
is Some(v_i)
. Otherwise returns None
.
def traverseX(f: a -> Option[b] \ ef, l: List[a]): Option[Unit] \ ef
SourceReturns Some()
if each of f(l_i)
is Some(_)
. 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 unzip(o: Option[(a, b)]): (Option[a], Option[b])
SourceReturns (Some(v1), Some(v2))
if o
is Some((v1, v2))
. Otherwise returns (None, None)
.
def withDefault(default: { default = Option[a] }, o: Option[a]): Option[a]
SourceReturns o
if it is Some(v)
. Otherwise returns default
.
def zip(o1: Option[a], o2: Option[b]): Option[(a, b)]
SourceReturns Some((v1, v2))
if o1
is Some(v1)
and o2
is Some(v2)
. Otherwise returns None
.