diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/option.gleam | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/gleam/option.gleam b/src/gleam/option.gleam index 663cd1f..46327ed 100644 --- a/src/gleam/option.gleam +++ b/src/gleam/option.gleam @@ -85,3 +85,24 @@ pub fn unwrap(option: Option(a), or default: a) -> a { None -> default } } + +/// Update a value held within the Some of an Option by calling a given function +/// on it. +/// +/// If the option is a None rather than Some the function is not called and the +/// option stays the same. +/// +/// ## Examples +/// +/// > map(over: Some(1), with: fn(x) { x + 1 }) +/// Some(2) +/// +/// > map(over: None, with: fn(x) { x + 1 }) +/// None +/// +pub fn map(over option: Option(a), with fun: fn(a) -> b) -> Option(b) { + case option { + Some(x) -> Some(fun(x)) + None -> None + } +} |