Optional values
flatMap[T, U](opt: Option[T], f: ( T) => Option[U]): Option[U]
Returns the result of the function f
applied to the value of opt
if opt
is Some
, or None
otherwise.
Usage example:
in x: Events[Option[Int]]
def f(a: Int) = if a < 5 then Some(a * 2) else None[Int]
def mapped := slift1(x, (opt: Option[Int]) => Option.flatMap(opt, f))
out mapped
Trace example:
option timeDomain: [-1,10]
stream x: bubbles
stream mapped: signal
---
1: x = 2
1: mapped = 4
3: x = —
3: mapped = —
6: x = 7
6: mapped = —
8: x = 4
8: mapped = 8
Note that in this example, Some
are represented as their value, and None
as —
map[T, U](opt: Option[T], f: ( T) => U): Option[U]
Returns the function f
applied to the value of opt
wrapped in Some
if opt
is Some
, or None
otherwise
Usage example:
in x: Events[Option[Int]]
def f(a: Int) = a * 2
def mapped := slift1(x, (opt: Option[Int]) => Option.map(opt, f))
out mapped
Trace example:
option timeDomain: [-1,10]
stream x: bubbles
stream mapped: signal
---
1: x = 2
1: mapped = 4
3: x = —
3: mapped = —
6: x = 1
6: mapped = 2
8: x = 4
8: mapped = 8
Note that in this example, Some
are represented as their value, and None
as —
map2[T, U, V](opt1: Option[T], opt2: Option[U], f: ( T, U) => V): Option[V]
Applies the function f
to the value of opt1
and opt2
and wraps it in Some
if opt1
and opt2
are both Some
or None
otherwise.
Usage example:
in x: Events[Int]
in y: Events[Int]
def f(a: Int, b: Int) = a + b
def mapped := lift(x, y, (opt1: Option[Int], opt2: Option[Int]) => Option.map2(opt1, opt2, f))
out mapped
Trace example:
option timeDomain: [-1,10]
stream x: bubbles
stream y: bubbles
stream mapped: bubbles
---
1: x = 2
1: y = 3
1: mapped = 5
3: x = 4
5: y = 1
8: x = 7
8: y = 2
8: mapped = 9