aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAhmad Sattar <thehabbos007@gmail.com>2020-06-17 22:12:52 +0200
committerLouis Pilfold <louis@lpil.uk>2020-06-18 21:54:19 +0100
commit75a5d752341cef2e35ec82752ab9b34539dba2a4 (patch)
tree719948a124c5d3cd026ff7e71730c0db78a34b7c /src
parent8b7f2290c67d7fc8714425785aaa14bcd781623c (diff)
downloadgleam_stdlib-75a5d752341cef2e35ec82752ab9b34539dba2a4.tar.gz
gleam_stdlib-75a5d752341cef2e35ec82752ab9b34539dba2a4.zip
Option map function
Diffstat (limited to 'src')
-rw-r--r--src/gleam/option.gleam21
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
+ }
+}