diff options
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | src/gleam/list.gleam | 18 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 6 |
3 files changed, 28 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index f983e6c..affe0aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- The `list` module gains the `each` function. + ## v0.11.0 - 2020-08-22 - Fix `uri.parse_query` to handle the case where query parameters are present diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index d9296da..4ca39f4 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1034,3 +1034,21 @@ pub fn each(list: List(a), f: fn(a) -> b) -> Nil { } } } + +pub fn partition( + list: List(a), + with categorise: fn(a) -> Bool, +) -> tuple(List(a), List(a)) { + do_partition(list, categorise, [], []) +} + +fn do_partition(list, categorise, trues, falses) { + case list { + [] -> tuple(reverse(trues), reverse(falses)) + [x, ..xs] -> + case categorise(x) { + True -> do_partition(xs, categorise, [x, ..trues], falses) + False -> do_partition(xs, categorise, trues, [x, ..falses]) + } + } +} diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index eff9fac..ff94c3b 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -475,3 +475,9 @@ pub fn key_set_test() { |> list.key_set(1, 100) |> should.equal([tuple(5, 0), tuple(4, 1), tuple(1, 100)]) } + +pub fn partition_test() { + [1, 2, 3, 4, 5, 6, 7] + |> list.partition(int.is_odd) + |> should.equal(tuple([1, 3, 5, 7], [2, 4, 6])) +} |