From 08307aa7d2beede0235b125d67ee6e5d302fd2b7 Mon Sep 17 00:00:00 2001 From: lareii Date: Wed, 25 Oct 2023 16:50:58 +0300 Subject: [PATCH] math: add sum --- DIRECTORY.md | 1 + math/sum.jule | 9 +++++++++ tests/math/package_test.jule | 1 + tests/math/sum_test.jule | 10 ++++++++++ 4 files changed, 21 insertions(+) create mode 100644 math/sum.jule create mode 100644 tests/math/sum_test.jule diff --git a/DIRECTORY.md b/DIRECTORY.md index 2833f08..5a68a3f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -7,6 +7,7 @@ - [Max](./math/max.jule) - [Median](./math/median.jule) - [Min](./math/min.jule) + - [Sum](./math/sum.jule) ## Search - [Binary Search](./search/binary_search.jule) diff --git a/math/sum.jule b/math/sum.jule new file mode 100644 index 0000000..4331391 --- /dev/null +++ b/math/sum.jule @@ -0,0 +1,9 @@ +pub fn sum(slice: []int): int { + let mut total: int = 0 + + for _, i in slice { + total += i + } + + ret total +} \ No newline at end of file diff --git a/tests/math/package_test.jule b/tests/math/package_test.jule index 4a82463..501fca8 100644 --- a/tests/math/package_test.jule +++ b/tests/math/package_test.jule @@ -6,4 +6,5 @@ pub fn math_package_tests(mut &tests: TestStack) { max_tests(tests) median_tests(tests) min_tests(tests) + sum_tests(tests) } diff --git a/tests/math/sum_test.jule b/tests/math/sum_test.jule new file mode 100644 index 0000000..e13149d --- /dev/null +++ b/tests/math/sum_test.jule @@ -0,0 +1,10 @@ +use tests::{TestStack} +use math::{sum} + +fn sum_tests(mut &tests: TestStack) { + let slice = [-9, 0, 1, 99, 54, 12] + + tests.add("[math] sum", fn(): bool { + ret sum(clone(slice)) == 157 + }) +}