aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian Porto <s@porto5.com>2021-04-08 21:48:12 +1000
committerLouis Pilfold <louis@lpil.uk>2021-04-09 10:39:19 +0100
commit895bb26dd125d946653c53897e73bdf88ae5555d (patch)
tree649b6f423fc4c8bd419230ee593421a423e940d8 /src
parent8d83506941b16722aed6eab102565bfd163ba053 (diff)
downloadgleam_stdlib-895bb26dd125d946653c53897e73bdf88ae5555d.tar.gz
gleam_stdlib-895bb26dd125d946653c53897e73bdf88ae5555d.zip
Add list.map_reduce
Diffstat (limited to 'src')
-rw-r--r--src/gleam/list.gleam30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 7df9862..6d9c709 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -246,6 +246,36 @@ pub fn map(list: List(a), with fun: fn(a) -> b) -> List(b) {
do_map(list, fun, [])
}
+/// Similar to map but also lets you pass around an accumulated value.
+///
+/// ## Examples
+///
+/// ```
+/// > map_reduce(
+/// over: [1, 2, 3],
+/// from: 100,
+/// with: fn(memo, i) { tuple(memo + i, i * 2) }
+/// )
+/// tuple(106, [2, 4, 6])
+/// ```
+///
+pub fn map_reduce(
+ over list: List(a),
+ from memo: memo,
+ with fun: fn(memo, a) -> tuple(memo, b),
+) -> tuple(memo, List(b)) {
+ fold(
+ over: list,
+ from: tuple(memo, []),
+ with: fn(item, acc) {
+ let tuple(current_memo, items) = acc
+ let tuple(next_memo, next_item) = fun(current_memo, item)
+ tuple(next_memo, [next_item, ..items])
+ },
+ )
+ |> pair.map_second(reverse)
+}
+
fn do_index_map(
list: List(a),
fun: fn(Int, a) -> b,