aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorlrosa007 <lrosa008@gmail.com>2020-11-04 15:50:27 -0500
committerLouis Pilfold <louis@lpil.uk>2020-11-04 21:03:21 +0000
commitbe647cae5e1bbda65d933fd5728e21a0fa7a4285 (patch)
treeb2f28fbd9cdea6adee4aaf39536be8dfb16b936d /src
parent0b32376508b229cdd2a23745d6fe2fbb436162aa (diff)
downloadgleam_stdlib-be647cae5e1bbda65d933fd5728e21a0fa7a4285.tar.gz
gleam_stdlib-be647cae5e1bbda65d933fd5728e21a0fa7a4285.zip
feat(int): add sum function
Diffstat (limited to 'src')
-rw-r--r--src/gleam/int.gleam19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index f717eaa..0bbd63d 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -148,3 +148,22 @@ pub fn is_odd(x: Int) -> Bool {
pub fn negate(x: Int) -> Int {
-1 * x
}
+
+/// Sums a list of Ints.
+///
+/// ## Example
+///
+/// > sum([1, 2, 3])
+/// 6
+///
+pub fn sum(numbers: List(Int)) -> Int {
+ numbers
+ |> do_sum(0)
+}
+
+fn do_sum(numbers: List(Int), initial: Int) -> Int {
+ case numbers {
+ [] -> initial
+ [x, ..rest] -> do_sum(rest, x + initial)
+ }
+}