option
The option module is auto-imported — no import statement needed.
Functions
Section titled “Functions”| Function | Signature | Description |
|---|---|---|
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) -> A | Get the inner value, or return a default if none. |
unwrap_or_else | (Option[A], Fn[Unit] -> A) -> A | Get the inner value, or compute a default using a function. |
is_some | (Option[A]) -> Bool | Check if the Option contains a value. |
is_none | (Option[A]) -> Bool | Check 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 []. |
Reference
Section titled “Reference”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) // => 0option.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) // => 42option.is_some(o: Option[A]) -> Bool
Section titled “option.is_some(o: Option[A]) -> Bool”Check if the Option contains a value.
option.is_some(some(42)) // => trueoption.is_none(o: Option[A]) -> Bool
Section titled “option.is_none(o: Option[A]) -> Bool”Check if the Option is none.
option.is_none(none) // => trueoption.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)option.to_list(o: Option[A]) -> List[A]
Section titled “option.to_list(o: Option[A]) -> List[A]”Convert some(x) to [x], none to [].
option.to_list(some(42)) // => [42]