aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <s@porto5.com>2021-01-12 09:09:29 +1100
committerLouis Pilfold <louis@lpil.uk>2021-01-13 17:01:09 +0000
commit955226b8ba73a51c7f4803f6ad54a75daf75ae24 (patch)
treeffd9e3b6fbb3280de63596053b6861655876d97a
parentb8000c4ef6100d05ff7912aa0bb59ccb0ce1063a (diff)
downloadgleam_stdlib-955226b8ba73a51c7f4803f6ad54a75daf75ae24.tar.gz
gleam_stdlib-955226b8ba73a51c7f4803f6ad54a75daf75ae24.zip
Add list.index_fold
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/list.gleam40
-rw-r--r--test/gleam/list_test.gleam6
3 files changed, 47 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 433f82e..1362050 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,7 +7,7 @@
- The `result` module gains the `lazy_or`, `lazy_unwrap`, and `replace_error` functions.
- The `bool` module gains the `nand`, `nor`, `exclusive_nor`, and `exclusive_or` functions.
- The `bit_builder` module gains the `from_string_builder` function.
-- The `list` modules gains the `permutations` function.
+- The `list` modules gains the `index_fold`, and `permutations` functions.
- Breaking change in `queue.from_list`. The head element in the list becomes the first element in the queue.
- Fix `queue.pop_back` and `queue.pop_front`
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 44d1510..4109df5 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -454,6 +454,46 @@ pub fn fold_right(list: List(a), from initial: b, with fun: fn(a, b) -> b) -> b
}
}
+/// Like fold but the folding function also receives the index of the current element.
+///
+/// ## Examples
+///
+/// ```
+/// ["a", "b", "c"]
+/// |> list.index_fold([], fn(index, item, acc) { ... })
+/// ```
+///
+pub fn index_fold(
+ over over: List(a),
+ from initial: b,
+ with fun: fn(Int, a, b) -> b,
+) -> b {
+ over
+ |> index_map(fn(ix, value) { tuple(ix, value) })
+ |> fold(
+ from: initial,
+ with: fn(t, acc) {
+ let tuple(ix, val) = t
+ fun(ix, val, acc)
+ },
+ )
+}
+
+pub fn try_fold(
+ over collection: List(a),
+ from accumulator: b,
+ with fun: fn(a, b) -> Result(b, b),
+) -> b {
+ case collection {
+ [] -> accumulator
+ [first, ..rest] ->
+ case fun(first, accumulator) {
+ Ok(next_accumulator) -> try_fold(rest, next_accumulator, fun)
+ Error(b) -> b
+ }
+ }
+}
+
/// Find the first element in a given list for which the given function returns
/// True.
///
diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam
index d9a4285..1c32878 100644
--- a/test/gleam/list_test.gleam
+++ b/test/gleam/list_test.gleam
@@ -171,6 +171,12 @@ pub fn fold_right_test() {
|> should.equal([1, 2, 3])
}
+pub fn index_fold_test() {
+ ["a", "b", "c"]
+ |> list.index_fold([], fn(ix, i, acc) { [tuple(ix, i), ..acc] })
+ |> should.equal([tuple(2, "c"), tuple(1, "b"), tuple(0, "a")])
+}
+
pub fn find_map_test() {
let f = fn(x) {
case x {