From 095d936bfcbf239d84e329b267aea0d6ebf15d33 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 17 Dec 2020 14:34:20 +1100 Subject: Add int.absolute_value --- src/gleam/int.gleam | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src') diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 002a155..a4a956a 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -3,6 +3,23 @@ import gleam/order.{Order} pub type Int = Int +/// Returns the absolute value of the input. +/// +/// ## Examples +/// +/// > absolute_value(-12) +/// 12 +/// +/// > absolute_value(10) +/// 10 +/// +pub fn absolute_value(num: Int) -> Int { + case num >= 0 { + True -> num + False -> num * -1 + } +} + /// Parse a given string as an int if possible. /// /// ## Examples -- cgit v1.2.3