Std\Option
Optional values — represents a value that may or may not exist.
Use Some value to wrap a value, None for absence. Chain operations
with flatMap, filter with predicates, or provide defaults with unwrapOr.
Option
Section titled “Option”type Option a = Some a | None
An optional value: either Some value or None.
Functions
Section titled “Functions”isSome : Option a -> Bool
Section titled “isSome : Option a -> Bool”Returns true if the option contains a value.
isSome (Some 42) # => trueisSome None # => falseisNone : Option a -> Bool
Section titled “isNone : Option a -> Bool”Returns true if the option is empty.
isNone None # => trueisNone (Some 42) # => falseunwrapOr : a -> Option a -> a
Section titled “unwrapOr : a -> Option a -> a”Extracts the value, or returns default if empty.
unwrapOr 0 (Some 42) # => 42unwrapOr 0 None # => 0map : (a -> b) -> Option a -> Option b
Section titled “map : (a -> b) -> Option a -> Option b”Transforms the contained value with fn, leaving None unchanged.
map (\x -> x * 2) (Some 5) # => Some 10map (\x -> x * 2) None # => NoneflatMap : (a -> b) -> Option a -> c
Section titled “flatMap : (a -> b) -> Option a -> c”Applies fn which itself returns an Option, flattening the result.
Useful for chaining operations that may fail.
flatMap (\x -> if x > 0 then Some (x * 10) else None) (Some 5) # => Some 50flatMap (\x -> if x > 0 then Some (x * 10) else None) (Some 0) # => Nonefilter : (a -> Bool) -> Option a -> Option a
Section titled “filter : (a -> Bool) -> Option a -> Option a”Keeps the value only if it satisfies pred, otherwise returns None.
filter (\x -> x > 3) (Some 5) # => Some 5filter (\x -> x > 3) (Some 1) # => NoneorElse : a -> Option a -> Option a
Section titled “orElse : a -> Option a -> Option a”Returns this option if it contains a value, otherwise returns alternative.
orElse (Some 99) None # => Some 99orElse (Some 99) (Some 42) # => Some 42toResult : a -> Option a -> (b, c)
Section titled “toResult : a -> Option a -> (b, c)”Converts to a Result: Some v becomes (:ok, v), None becomes (:err, err).
toResult "missing" (Some 42) # => (:ok, 42)toResult "missing" None # => (:err, "missing")zip : Option a -> Option a -> Option a
Section titled “zip : Option a -> Option a -> Option a”Combines two options into an option of a pair. Returns None if either is empty.
zip (Some 1) (Some 2) # => Some (1, 2)zip (Some 1) None # => Nonefold : a -> (b -> c) -> Option b -> a
Section titled “fold : a -> (b -> c) -> Option b -> a”Eliminates an option: returns onNone if empty, applies onSome if present.
fold 0 (\x -> x * 10) (Some 5) # => 50fold 0 (\x -> x * 10) None # => 0