Option
Utilities for working with the Option data type.
Edit on GitHubThe Option type is an enum that represents the possibility of something being present (with the Some variant), or not (with the None variant). There’s no standalone null or nil type in Grain; use an Option where you would normally reach for null or nil.
Added in 0.2.0
No other changes yet.
from "option" include Option
let hasValue = Some(1234) // Creates an Option containing 1234
let noValue = None // Creates an Option containing nothing
Functions and constants included in the Option module.
Added in 0.2.0
No other changes yet.
isSome : (option: Option<a>) => Bool
Checks if the Option is the Some variant.
Parameters:
| param | type | description |
|---|---|---|
option | Option<a> | The option to check |
option: The option to checkReturns:
| type | description |
|---|---|
Bool | true if the Option is the Some variant or false otherwise |
Bool: true if the Option is the Some variant or false otherwiseAdded in 0.2.0
No other changes yet.
isNone : (option: Option<a>) => Bool
Checks if the Option is the None variant.
Parameters:
| param | type | description |
|---|---|---|
option | Option<a> | The option to check |
option: The option to checkReturns:
| type | description |
|---|---|
Bool | true if the Option is the None variant or false otherwise |
Bool: true if the Option is the None variant or false otherwiseAdded in 0.2.0
No other changes yet.
contains : (value: a, option: Option<a>) => Bool
Checks if the Option is the Some variant and contains the given value. Uses the generic == equality operator.
Parameters:
| param | type | description |
|---|---|---|
value | a | The value to search for |
option | Option<a> | The option to search |
value: The value to search foroption: The option to searchReturns:
| type | description |
|---|---|
Bool | true if the Option is equivalent to Some(value) or false otherwise |
Bool: true if the Option is equivalent to Some(value) or false otherwiseAdded in 0.2.0
No other changes yet.
expect : (msg: String, option: Option<a>) => a
Extracts the value inside a Some option, otherwise throws an
exception containing the message provided.
Parameters:
| param | type | description |
|---|---|---|
msg | String | The message to use upon failure |
option | Option<a> | The option to extract a value from |
msg: The message to use upon failureoption: The option to extract a value fromReturns:
| type | description |
|---|---|
a | The unwrapped value if the Option is the Some variant |
a: The unwrapped value if the Option is the Some variantThrows:
Failure(String)
- When the
optionisNone
Added in 0.2.0
No other changes yet.
unwrap : (option: Option<a>) => a
Extracts the value inside a Some option, otherwise
throws an exception containing a default message.
Parameters:
| param | type | description |
|---|---|---|
option | Option<a> | The option to extract the value from |
option: The option to extract the value fromReturns:
| type | description |
|---|---|
a | The unwrapped value if the Option is the Some variant |
a: The unwrapped value if the Option is the Some variantThrows:
Failure(String)
- When the
optionisNone
Added in 0.2.0
No other changes yet.
unwrapWithDefault : (default: a, option: Option<a>) => a
Extracts the value inside a Some option or provide the default value if None.
Parameters:
| param | type | description |
|---|---|---|
default | a | The default value |
option | Option<a> | The option to unwrap |
default: The default valueoption: The option to unwrapReturns:
| type | description |
|---|---|
a | The unwrapped value if the Option is the Some variant or the default value otherwise |
a: The unwrapped value if the Option is the Some variant or the default value otherwiseAdded in 0.2.0
No other changes yet.
map : (fn: (a => b), option: Option<a>) => Option<b>
If the Option is Some(value), applies the given function to the value and wraps the new value in a Some variant.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of a Some variant |
option | Option<a> | The option to map |
fn: The function to call on the value of a Some variantoption: The option to mapReturns:
| type | description |
|---|---|
Option<b> | A new Some variant produced by the mapping function if the variant was Some or the unmodified None otherwise |
Option<b>: A new Some variant produced by the mapping function if the variant was Some or the unmodified None otherwiseAdded in 0.2.0
No other changes yet.
mapWithDefault : (fn: (a => b), default: b, option: Option<a>) => b
If the Option is Some(value), applies the given function to the value to produce a new value, otherwise uses the default value.
Useful for unwrapping an Option while providing a fallback for any None variants.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of a Some variant |
default | b | A fallback value for a None variant |
option | Option<a> | The option to map |
fn: The function to call on the value of a Some variantdefault: A fallback value for a None variantoption: The option to mapReturns:
| type | description |
|---|---|
b | The value produced by the mapping function if the Option is of the Some variant or the default value otherwise |
b: The value produced by the mapping function if the Option is of the Some variant or the default value otherwiseAdded in 0.2.0
No other changes yet.
mapWithDefaultFn :
(fn: (a => b), defaultFn: (() => b), option: Option<a>) => b
If the Option is Some(value), applies the fn function to the value to produce a new value.
If the Option is None, calls the defaultFn function to produce a new value.
Useful for unwrapping an Option into a value, whether it is Some or None.
Parameters:
| param | type | description |
|---|---|---|
fn | a => b | The function to call on the value of a Some variant |
defaultFn | () => b | The default function |
option | Option<a> | The option to map |
fn: The function to call on the value of a Some variantdefaultFn: The default functionoption: The option to mapReturns:
| type | description |
|---|---|
b | The value produced by one of the mapping functions |
b: The value produced by one of the mapping functionsAdded in 0.2.0
No other changes yet.
flatMap : (fn: (a => Option<b>), option: Option<a>) => Option<b>
If the Option is Some(value), applies the given function to the value to produce a new Option.
Parameters:
| param | type | description |
|---|---|---|
fn | a => Option<b> | The function to call on the value of a Some variant |
option | Option<a> | The option to map |
fn: The function to call on the value of a Some variantoption: The option to mapReturns:
| type | description |
|---|---|
Option<b> | A new Option produced by the mapping function if the variant was Some or the unmodified None otherwise |
Option<b>: A new Option produced by the mapping function if the variant was Some or the unmodified None otherwiseAdded in 0.2.0
No other changes yet.
filter : (fn: (a => Bool), option: Option<a>) => Option<a>
Converts Some(value) variants to None variants where the predicate function returns false.
if the fn return true returns Some(value), otherwise returns None.
Parameters:
| param | type | description |
|---|---|---|
fn | a => Bool | The predicate function to indicate if the option should remain Some |
option | Option<a> | The option to inspect |
fn: The predicate function to indicate if the option should remain Someoption: The option to inspectReturns:
| type | description |
|---|---|
Option<a> | Some(value) if the variant was Some and the predicate returns true or None otherwise |
Option<a>: Some(value) if the variant was Some and the predicate returns true or None otherwiseAdded in 0.2.0
No other changes yet.
zip : (optionA: Option<a>, optionB: Option<b>) => Option<(a, b)>
Combine two Options into a single Option containing a tuple of their values.
Parameters:
| param | type | description |
|---|---|---|
optionA | Option<a> | The first option to combine |
optionB | Option<b> | The second option to combine |
optionA: The first option to combineoptionB: The second option to combineReturns:
| type | description |
|---|---|
Option<(a, b)> | Some((valueA, valueB)) if both Options are Some variants or None otherwise |
Option<(a, b)>: Some((valueA, valueB)) if both Options are Some variants or None otherwiseAdded in 0.2.0
No other changes yet.
zipWith :
(fn: ((a, b) => c), optionA: Option<a>, optionB: Option<b>) => Option<c>
Combine two Options into a single Option. The new value is produced by applying the given function to both values.
Parameters:
| param | type | description |
|---|---|---|
fn | (a, b) => c | The function to generate a new value |
optionA | Option<a> | The first option to combine |
optionB | Option<b> | The second option to combine |
fn: The function to generate a new valueoptionA: The first option to combineoptionB: The second option to combineReturns:
| type | description |
|---|---|
Option<c> | Some(newValue) if both Options are Some variants or None otherwise |
Option<c>: Some(newValue) if both Options are Some variants or None otherwiseAdded in 0.2.0
No other changes yet.
flatten : (option: Option<Option<a>>) => Option<a>
Flattens nested Options.
Parameters:
| param | type | description |
|---|---|---|
option | Option<Option<a>> | The option to flatten |
option: The option to flattenReturns:
| type | description |
|---|---|
Option<a> | Some(innerValue) if all nested options were the Some variant or None otherwise |
Option<a>: Some(innerValue) if all nested options were the Some variant or None otherwiseExamples:
Option.flatten(Some(Some(1))) == Some(1)
Added in 0.2.0
No other changes yet.
toList : (option: Option<a>) => List<a>
Converts an Option to a list with either zero or one item.
Parameters:
| param | type | description |
|---|---|---|
option | Option<a> | The option to convert |
option: The option to convertReturns:
| type | description |
|---|---|
List<a> | [value] if the Option was the Some variant or [] otherwise |
List<a>: [value] if the Option was the Some variant or [] otherwiseAdded in 0.2.0
No other changes yet.
toArray : (option: Option<a>) => Array<a>
Converts an Option to an array with either zero or one item.
Parameters:
| param | type | description |
|---|---|---|
option | Option<a> | The option to convert |
option: The option to convertReturns:
| type | description |
|---|---|
Array<a> | [> value] if the Option was the Some variant or [> ] otherwise |
Array<a>: [> value] if the Option was the Some variant or [> ] otherwiseAdded in 0.2.0
No other changes yet.
toResult : (err: a, option: Option<b>) => Result<b, a>
Converts the Option to a Result, using the provided error in case of the None variant.
Parameters:
| param | type | description |
|---|---|---|
err | a | The error to use if the option is None |
option | Option<b> | The option to convert |
err: The error to use if the option is Noneoption: The option to convertReturns:
| type | description |
|---|---|
Result<b, a> | Ok(value) if the Option is Some(value) or Err(err) if the Option is None |
Result<b, a>: Ok(value) if the Option is Some(value) or Err(err) if the Option is NoneAdded in 0.2.0
No other changes yet.
sideEffect : (fn: (a => Void), option: Option<a>) => Void
If the Option is Some(value), applies the fn function to the value without producing a new value.
Parameters:
| param | type | description |
|---|---|---|
fn | a => Void | The function to call on the value of a Some variant |
option | Option<a> | The option to inspect |
fn: The function to call on the value of a Some variantoption: The option to inspectAdded in 0.2.0
No other changes yet.
peek : (fn: (a => Void), option: Option<a>) => Option<a>
If the Option is Some(value), applies the fn function to the value without producing a new value.
Useful for inspecting Options without changing anything.
Parameters:
| param | type | description |
|---|---|---|
fn | a => Void | The function to call on the value of a Some variant |
option | Option<a> | The option to inspect |
fn: The function to call on the value of a Some variantoption: The option to inspectReturns:
| type | description |
|---|---|
Option<a> | The unmodified option |
Option<a>: The unmodified optionAdded in 0.6.0
| version | changes |
|---|---|
0.2.0 | Originally named `or` |
(||) : (optionA: Option<a>, optionB: Option<a>) => Option<a>
Behaves like a logical OR (||) where the first Option is only returned if it is the Some variant and falling back to the second Option in all other cases.
Parameters:
| param | type | description |
|---|---|---|
optionA | Option<a> | The first option |
optionB | Option<a> | The second option |
optionA: The first optionoptionB: The second optionReturns:
| type | description |
|---|---|
Option<a> | The first Option if it is the Some variant or the second Option otherwise |
Option<a>: The first Option if it is the Some variant or the second Option otherwiseAdded in 0.6.0
| version | changes |
|---|---|
0.2.0 | Originally named `and` |
(&&) : (optionA: Option<a>, optionB: Option<a>) => Option<a>
Behaves like a logical AND (&&) where the first Option is only returned if it is the None variant and falling back to the second Option Result in all other cases.
Parameters:
| param | type | description |
|---|---|---|
optionA | Option<a> | The first option |
optionB | Option<a> | The second option |
optionA: The first optionoptionB: The second optionReturns:
| type | description |
|---|---|
Option<a> | The second Option if both are the Some variant or the first Option otherwise |
Option<a>: The second Option if both are the Some variant or the first Option otherwise