aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Porto <s@porto5.com>2021-04-09 16:52:49 +1000
committerLouis Pilfold <louis@lpil.uk>2021-04-09 10:39:19 +0100
commit71a8a5996c1eed0ffeea1283ddea5aa63686ce7c (patch)
treef446ab36d5dc8c933f4c5079c032d2475bd98353
parent64d1e4d24cd297e8dc59ee271ab5a92e05b11e90 (diff)
downloadgleam_stdlib-71a8a5996c1eed0ffeea1283ddea5aa63686ce7c.tar.gz
gleam_stdlib-71a8a5996c1eed0ffeea1283ddea5aa63686ce7c.zip
Flip order of args passed to fn in map_reduce. Item first and memo second.
-rw-r--r--src/gleam/list.gleam14
-rw-r--r--test/gleam/list_test.gleam4
2 files changed, 9 insertions, 9 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 6d9c709..e911b80 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -262,18 +262,18 @@ pub fn map(list: List(a), with fun: fn(a) -> b) -> List(b) {
pub fn map_reduce(
over list: List(a),
from memo: memo,
- with fun: fn(memo, a) -> tuple(memo, b),
-) -> tuple(memo, List(b)) {
+ with fun: fn(a, memo) -> tuple(b, memo),
+) -> tuple(List(b), memo) {
fold(
over: list,
- from: tuple(memo, []),
+ 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])
+ let tuple(items, current_memo) = acc
+ let tuple(next_item, next_memo) = fun(item, current_memo)
+ tuple([next_item, ..items], next_memo)
},
)
- |> pair.map_second(reverse)
+ |> pair.map_first(reverse)
}
fn do_index_map(
diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam
index fa87952..b7470ac 100644
--- a/test/gleam/list_test.gleam
+++ b/test/gleam/list_test.gleam
@@ -101,8 +101,8 @@ pub fn map_test() {
pub fn map_reduce_test() {
[1, 2, 3, 4]
- |> list.map_reduce(from: 0, with: fn(acc, i) { tuple(acc + i, i * 2) })
- |> should.equal(tuple(10, [2, 4, 6, 8]))
+ |> list.map_reduce(from: 0, with: fn(i, acc) { tuple(i * 2, acc + i) })
+ |> should.equal(tuple([2, 4, 6, 8], 10))
}
pub fn try_map_test() {