From 75a5d752341cef2e35ec82752ab9b34539dba2a4 Mon Sep 17 00:00:00 2001 From: Ahmad Sattar Date: Wed, 17 Jun 2020 22:12:52 +0200 Subject: Option map function --- src/gleam/option.gleam | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') 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 + } +} -- cgit v1.2.3