Skip to content

option

The option module is auto-imported — no import statement needed.

FunctionSignatureDescription
map(Option[A], Fn[A] -> B) -> Option[B]Transform the inner value using a function. If none, returns none.
flat_map(Option[A], Fn[A] -> Option[B]) -> Option[B]Chain an Option-returning function on the inner value. Flattens nested Options.
flatten(Option[Option[A]]) -> Option[A]Flatten a nested Option. some(some(x)) becomes some(x), some(none) becomes none.
unwrap_or(Option[A], A) -> AGet the inner value, or return a default if none.
unwrap_or_else(Option[A], Fn[Unit] -> A) -> AGet the inner value, or compute a default using a function.
is_some(Option[A]) -> BoolCheck if the Option contains a value.
is_none(Option[A]) -> BoolCheck if the Option is none.
to_result(Option[A], String) -> Result[A, String]Convert some to ok, none to err with the given error message.
filter(Option[A], Fn[A] -> Bool) -> Option[A]Keep the value if it satisfies the predicate, otherwise return none.
zip(Option[A], Option[B]) -> Option[(A, B)]Combine two Options into an Option of a tuple. None if either is none.
or_else(Option[A], Fn[Unit] -> Option[A]) -> Option[A]Return the Option if some, otherwise call the function to produce an alternative.
to_list(Option[A]) -> List[A]Convert some(x) to [x], none to [].

option.map(o: Option[A], f: Fn[A] -> B) -> Option[B]

Section titled “option.map(o: Option[A], f: Fn[A] -> B) -> Option[B]”

Transform the inner value using a function. If none, returns none.

option.map(some(2), (x) => x * 10) // => some(20)

option.flat_map(o: Option[A], f: Fn[A] -> Option[B]) -> Option[B]

Section titled “option.flat_map(o: Option[A], f: Fn[A] -> Option[B]) -> Option[B]”

Chain an Option-returning function on the inner value. Flattens nested Options.

option.flat_map(some(5), (x) => if x > 0 then some(x) else none)

option.flatten(o: Option[Option[A]]) -> Option[A]

Section titled “option.flatten(o: Option[Option[A]]) -> Option[A]”

Flatten a nested Option. some(some(x)) becomes some(x), some(none) becomes none.

option.flatten(some(some(42))) // => some(42)

option.unwrap_or(o: Option[A], default: A) -> A

Section titled “option.unwrap_or(o: Option[A], default: A) -> A”

Get the inner value, or return a default if none.

option.unwrap_or(none, 0) // => 0

option.unwrap_or_else(o: Option[A], f: Fn[Unit] -> A) -> A

Section titled “option.unwrap_or_else(o: Option[A], f: Fn[Unit] -> A) -> A”

Get the inner value, or compute a default using a function.

option.unwrap_or_else(none, () => 42) // => 42

Check if the Option contains a value.

option.is_some(some(42)) // => true

Check if the Option is none.

option.is_none(none) // => true

option.to_result(o: Option[A], err: String) -> Result[A, String]

Section titled “option.to_result(o: Option[A], err: String) -> Result[A, String]”

Convert some to ok, none to err with the given error message.

option.to_result(some(42), "missing") // => ok(42)

option.filter(o: Option[A], f: Fn[A] -> Bool) -> Option[A]

Section titled “option.filter(o: Option[A], f: Fn[A] -> Bool) -> Option[A]”

Keep the value if it satisfies the predicate, otherwise return none.

option.filter(some(5), (x) => x > 3) // => some(5)

option.zip(a: Option[A], b: Option[B]) -> Option[(A, B)]

Section titled “option.zip(a: Option[A], b: Option[B]) -> Option[(A, B)]”

Combine two Options into an Option of a tuple. None if either is none.

option.zip(some(1), some(2)) // => some((1, 2))

option.or_else(o: Option[A], f: Fn[Unit] -> Option[A]) -> Option[A]

Section titled “option.or_else(o: Option[A], f: Fn[Unit] -> Option[A]) -> Option[A]”

Return the Option if some, otherwise call the function to produce an alternative.

option.or_else(none, () => some(42)) // => some(42)

Convert some(x) to [x], none to [].

option.to_list(some(42)) // => [42]