Option

GitHub   Edit on GitHub

Utilities for working with the Option data type.

The Option type is a special type of enum that exists to represent the possibility of something being present (with the Some variant), or not (with the None variant). There’s no standalone null or none type in Grain; use an Option where you would normally reach for null or none.

1
import Option from "option"

Values

Option.isSome

1
isSome : Option<a> -> Bool

Checks if the Option is the Some variant.

Option.isNone

1
isNone : Option<a> -> Bool

Checks if the Option is the None variant.

Option.contains

1
contains : (a, Option<a>) -> Bool

Checks if the Option is the Some variant and contains the given value. Uses the generic == equality operator.

Option.expect

1
expect : (String, Option<a>) -> a

Attempts to unwrap the inner value from the Some variant. If it is None, raises Failure with the provided message.

Option.unwrap

1
unwrap : Option<a> -> a

Attempts to unwrap the inner value from the Some variant. If it is None, raises Failure with a generic message.

Option.unwrapWithDefault

1
unwrapWithDefault : (a, Option<a>) -> a

Unwraps the inner value from the Some variant. If it is None, returns the default value provided.

Option.map

1
map : (a -> b, Option<a>) -> Option<b>

If the Option is the Some variant, call fn with the inner value and returns a new Some variant with the produced value.

Option.mapWithDefault

1
mapWithDefault : (a -> b, b, Option<a>) -> b

If the Option is the Some variant, call fn with the inner value and returns the result. If it is None, returns the default value provided.

Option.mapWithDefaultFn

1
mapWithDefaultFn : (a -> b, () -> b, Option<a>) -> b

If the Option is the Some variant, call fn with the inner value and returns the result. If it is None, returns the result of the default function provided.

Option.flatMap

1
flatMap : (a -> Option<b>, Option<a>) -> Option<b>

If the Option is the Some variant, call fn with the inner value. The fn must produce its own Option to be returned.

Option.filter

1
filter : (a -> Bool, Option<a>) -> Option<a>

If the Option is the Some variant, call fn with the inner value. If the fn returns false, returns a None variant type.

Option.zip

1
zip : (Option<a>, Option<b>) -> Option<(a, b)>

If both Options are the Some variant, returns a new Some variant that contains a tuple of both values. If any of the Options are None, returns None.

Option.zipWith

1
zipWith : ((a, b) -> c, Option<a>, Option<b>) -> Option<c>

If both Options are the Some variant, call fn with both inner values and returns a new Some variant with the produced value. If any of the Options are None, returns None.

Option.flatten

1
flatten : Option<Option<a>> -> Option<a>

Flattens nested Options, like Some(Some(1)) to Some(1). If any of the Options are None, returns None.

Option.toList

1
toList : Option<a> -> List<a>

If the Option is the Some variant, returns a List containing the inner value as the only item. If it is None, returns an empty List.

Option.toArray

1
toArray : Option<a> -> Array<a>

If the Option is the Some variant, returns an Array containing the inner value as the only item. If it is None, returns an empty Array.

Option.toResult

1
toResult : (b, Option<a>) -> Result<a, b>

If the Option is the Some(a), returns Ok(a). If it is None, returns an Err of the provided error value.

Option.sideEffect

1
sideEffect : (a -> Void, Option<a>) -> Void

If the Option is the Some variant, call fn with the inner value. Always returns void.

Option.peek

1
peek : (a -> Void, Option<a>) -> Option<a>

If the Option is the Some variant, call fn with the inner value. Always returns the Option it is called with; this method is a “chainable” Option.sideEffect.

Option.and

1
and : (Option<a>, Option<a>) -> Option<a>

If the first Option is the Some variant, returns the second Option. Returns None otherwise.

Option.or

1
or : (Option<a>, Option<a>) -> Option<a>

Returns the first Option if it is the Some variant. Returns the second Option otherwise.

This is a notification!