From 33bbf866ed3be72ccce955e5b2ee1bb99fa45e97 Mon Sep 17 00:00:00 2001 From: Al Dee Date: Fri, 22 May 2020 13:34:23 +0100 Subject: Add the Option type. --- src/gleam/option.gleam | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/gleam/option.gleam (limited to 'src') diff --git a/src/gleam/option.gleam b/src/gleam/option.gleam new file mode 100644 index 0000000..30bc1e2 --- /dev/null +++ b/src/gleam/option.gleam @@ -0,0 +1,38 @@ +/// Option represents a value that may be present or not. Some means the value is +/// present, None means the value is not. +/// +/// This is Gleam's alternative to having a value that could be Null, as is +/// possible in some other languages. +/// +pub type Option(a) { + Some(a) + None +} + +/// Check whether the option is a Some value. +/// +/// ## Examples +/// +/// > is_some(Some(1)) +/// True +/// +/// > is_some(None) +/// False +/// +pub fn is_some(option: Option(a)) -> Bool { + option != None +} + +/// Check whether the option is a None value. +/// +/// ## Examples +/// +/// > is_none(Some(1)) +/// False +/// +/// > is_none(None) +/// True +/// +pub fn is_none(option: Option(a)) -> Bool { + option == None +} -- cgit v1.2.3