Option
Option module: operations on Option(a) = Some(a) None
All functions are pure. The type Option(a) is a builtin with constructors Some and None.
Functions
Extracts the value from Some(x), panicking with the given message if None. Use when you are certain the option is Some.
The panic message is prefixed with the module name and on None so it is clearly attributable when it shows up in a stack trace (Option.expect on None: <msg>).
Examples
march> Option.expect(Some(3), "config must have a port")
3Returns None if the value does not satisfy pred, otherwise the option unchanged. A Some result is known to satisfy pred (an abstract refinement p, as for List.filter).
Examples
march> Option.filter(Some(4), fn x -> x % 2 == 0)
Some(4)
march> Option.filter(Some(3), fn x -> x % 2 == 0)
NoneApplies f (which itself returns an Option) and flattens the result. Use it to chain steps that can each fail.
Examples
march> Option.flat_map(Some(10), fn n -> if n > 0 do Some(100 / n) else None end)
Some(10)
march> Option.flat_map(Some(0), fn n -> if n > 0 do Some(100 / n) else None end)
None
march> Option.flat_map(None, fn n -> if n > 0 do Some(100 / n) else None end)
NoneReturns true if the option is None.
march> Option.is_none(None)
true
march> Option.is_none(Some(0))
falseReturns true if the option contains a value.
march> Option.is_some(Some(42))
true
march> Option.is_some(None)
falseApplies f to the contained value, or returns None.
Examples
march> Option.map(Some(2), fn x -> x * 10)
Some(20)
march> Option.map(None, fn x -> x * 10)
NoneReturns f() if None, or the original Some value (without calling f).
Examples
march> Option.or_else(Some(1), fn -> Some(2))
Some(1)
march> Option.or_else(None, fn -> Some(2))
Some(2)Converts Option(a) to a List: Some(x) becomes [x], None becomes [].
Examples
march> Option.to_list(Some(7))
[7]
march> Option.to_list(None)
[]Converts Option(a) to Result(a, e): Some(x) becomes Ok(x), None becomes Err(err).
Examples
march> Option.to_result(Some(1), "missing")
Ok(1)
march> Option.to_result(None, "missing")
Err("missing")Extracts the value from Some(x), panicking if None.
march> Option.unwrap(Some(42))
42
march> Option.unwrap(None)
** panic: Option.unwrap called on NoneReturns the contained value, or default if None.
march> Option.unwrap_or(Some(7), 0)
7
march> Option.unwrap_or(None, 99)
99Returns the contained value, or calls f to produce a default. f is only called when the option is None, so it can do work you want to skip on Some.
Examples
march> Option.unwrap_or_else(Some(5), fn -> 0)
5
march> Option.unwrap_or_else(None, fn -> 2 * 21)
42Combines two Options into a tuple Option, returning None if either is None.
Examples
march> Option.zip(Some(1), Some("a"))
Some((1, "a"))
march> Option.zip(Some(1), None)
None