March Docs

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

fnis_someis_some(opt : Option(a)) : Bool#

Returns true if the option contains a value.

march> Option.is_some(Some(42)) true

march> Option.is_some(None) false

fnis_noneis_none(opt : Option(a)) : Bool#

Returns true if the option is None.

march> Option.is_none(None) true

march> Option.is_none(Some(0)) false

fnexpectexpect(opt : Option(a), msg : String) : a#

Extracts the value from Some(x), panicking with the given message if None. Use when you are certain the option is Some.

fnunwrapunwrap(opt : Option(a)) : a#

Extracts the value from Some(x), panicking if None.

march> Option.unwrap(Some(42)) 42

march> Option.unwrap(None) panic: Option.unwrap called on None

fnunwrap_orunwrap_or(opt : Option(a), default : a) : a#

Returns the contained value, or default if None.

march> Option.unwrap_or(Some(7), 0) 7

march> Option.unwrap_or(None, 99) 99

fnunwrap_or_elseunwrap_or_else(opt : Option(a), f : () -> a) : a#

Returns the contained value, or calls f to produce a default.

fnmapmap(opt : Option(a), f : a -> b) : Option(b)#

Applies f to the contained value, or returns None.

fnflat_mapflat_map(opt : Option(a), f : a -> Option(b)) : Option(b)#

Applies f (which itself returns an Option) and flattens the result.

fnfilterfilter(opt : Option(a), pred : a -> Bool) : Option(a)#

Returns None if the value does not satisfy pred.

fnor_elseor_else(opt : Option(a), f : () -> Option(a)) : Option(a)#

Returns f() if None, or the original Some value.

fnzipzip(a : Option(a), b : Option(b)) : Option((a, b))#

Combines two Options into a tuple Option, returning None if either is None.

fnto_resultto_result(opt : Option(a), err : e) : Result(a, e)#

Converts Option(a) to Result(a, e) using the provided error value.

fnto_listto_list(opt : Option(a)) : List(a)#

Converts Option(a) to a List: Some(x) becomes [x], None becomes [].