TeSSLa Standard Library for TeSSLa 1.2.1

module OptionUtils

Optional values

ANCHOR flatMap

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 —

def flatMap[T, U](opt: Option[T], f: (T) => Option[U]): Option[U] =
  if isNone(opt)
  then None[U]
  else f(getSome(opt))

ANCHOR map

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 —

def map[T, U](opt: Option[T], f: (T) => U): Option[U] =
  if isNone(opt)
  then None[U]
  else Some(f(getSome(opt)))

ANCHOR map2

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
def map2[T,U,V](opt1: Option[T], opt2: Option[U], f: (T, U) => V): Option[V] =
  flatMap(opt1, (v1: T) => map(opt2, (v2: U) => f(v1, v2)))

ANCHOR toSet

liftable toSet[T](option: Option[T]): Set[T]

liftable def toSet[T](option: Option[T]): Set[T] = __private__Option_toSet(option)