diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-11 18:03:27 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-11 18:03:28 +0100 |
commit | f083720d6eaf1c68a7f70dfb168fd2a431671b25 (patch) | |
tree | a9569d039111a300aa30e6ddf9e5523efcb9ea19 /test | |
parent | 2ca0b25d3841dee7e12d3ae39520f8339fd1d5b7 (diff) | |
download | gleam_stdlib-f083720d6eaf1c68a7f70dfb168fd2a431671b25.tar.gz gleam_stdlib-f083720d6eaf1c68a7f70dfb168fd2a431671b25.zip |
Flip arguments of reducers
Closes https://github.com/gleam-lag/gleam/issues/1258
Diffstat (limited to 'test')
-rw-r--r-- | test/gleam/iterator_test.gleam | 35 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 18 | ||||
-rw-r--r-- | test/gleam/map_test.gleam | 4 |
3 files changed, 26 insertions, 31 deletions
diff --git a/test/gleam/iterator_test.gleam b/test/gleam/iterator_test.gleam index 84292e4..2debc51 100644 --- a/test/gleam/iterator_test.gleam +++ b/test/gleam/iterator_test.gleam @@ -1,10 +1,7 @@ import gleam/should import gleam/iterator.{Done, Next} import gleam/list - -if erlang { - import gleam/map -} +import gleam/map // a |> from_list |> to_list == a pub fn to_from_list_test() { @@ -81,7 +78,7 @@ pub fn fold_test() { |> should.equal(list.fold(subject, acc, f)) } - let f = fn(e, acc) { [e, ..acc] } + let f = fn(acc, e) { [e, ..acc] } test([], [], f) test([1], [], f) test([1, 2, 3], [], f) @@ -372,22 +369,20 @@ pub fn all_test() { |> should.be_false } -if erlang { - pub fn group_test() { - iterator.from_list([1, 2, 3, 4, 5, 6]) - |> iterator.group(by: fn(n) { n % 3 }) - |> should.equal(map.from_list([#(0, [3, 6]), #(1, [1, 4]), #(2, [2, 5])])) - } +pub fn group_test() { + iterator.from_list([1, 2, 3, 4, 5, 6]) + |> iterator.group(by: fn(n) { n % 3 }) + |> should.equal(map.from_list([#(0, [3, 6]), #(1, [1, 4]), #(2, [2, 5])])) +} - pub fn reduce_test() { - iterator.empty() - |> iterator.reduce(with: fn(x, y) { x + y }) - |> should.equal(Error(Nil)) +pub fn reduce_test() { + iterator.empty() + |> iterator.reduce(with: fn(x, y) { x + y }) + |> should.equal(Error(Nil)) - iterator.from_list([1, 2, 3, 4, 5]) - |> iterator.reduce(with: fn(x, y) { x + y }) - |> should.equal(Ok(15)) - } + iterator.from_list([1, 2, 3, 4, 5]) + |> iterator.reduce(with: fn(x, y) { x + y }) + |> should.equal(Ok(15)) } pub fn last_test() { @@ -439,7 +434,7 @@ pub fn fold_until_test() { |> should.equal(list.fold_until(subject, acc, f)) } - let f = fn(e, acc) { + let f = fn(acc, e) { case e { _ if e < 6 -> list.Continue([e, ..acc]) _ -> list.Stop(acc) diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index 62a99aa..b0d81c3 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -108,8 +108,8 @@ pub fn map_test() { pub fn map_fold_test() { [1, 2, 3, 4] - |> list.map_fold(from: 0, with: fn(i, acc) { #(i * 2, acc + i) }) - |> should.equal(#([2, 4, 6, 8], 10)) + |> list.map_fold(from: 0, with: fn(acc, i) { #(acc + i, i * 2) }) + |> should.equal(#(10, [2, 4, 6, 8])) } pub fn try_map_test() { @@ -200,19 +200,19 @@ pub fn flat_map_test() { pub fn fold_test() { [1, 2, 3] - |> list.fold([], fn(x, acc) { [x, ..acc] }) + |> list.fold([], fn(acc, x) { [x, ..acc] }) |> should.equal([3, 2, 1]) } pub fn fold_right_test() { [1, 2, 3] - |> list.fold_right(from: [], with: fn(x, acc) { [x, ..acc] }) + |> list.fold_right(from: [], with: fn(acc, x) { [x, ..acc] }) |> should.equal([1, 2, 3]) } pub fn index_fold_test() { ["a", "b", "c"] - |> list.index_fold([], fn(ix, i, acc) { [#(ix, i), ..acc] }) + |> list.index_fold([], fn(acc, i, ix) { [#(ix, i), ..acc] }) |> should.equal([#(2, "c"), #(1, "b"), #(0, "a")]) } @@ -220,7 +220,7 @@ pub fn fold_until_test() { [1, 2, 3, 4] |> list.fold_until( from: 0, - with: fn(n, acc) { + with: fn(acc, n) { case n < 4 { True -> list.Continue(acc + n) False -> list.Stop(acc) @@ -672,17 +672,17 @@ pub fn reduce_test() { pub fn scan_test() { [] - |> list.scan(from: 0, with: fn(i, acc) { i + acc }) + |> list.scan(from: 0, with: fn(acc, i) { i + acc }) |> should.equal([]) [1, 2, 3, 4] - |> list.scan(from: 0, with: fn(i, acc) { 2 * i + acc }) + |> list.scan(from: 0, with: fn(acc, i) { 2 * i + acc }) |> should.equal([2, 6, 12, 20]) [1, 2, 3, 4] |> list.scan( from: [], - with: fn(i, acc) { + with: fn(acc, i) { case int.is_even(i) { True -> ["Even", ..acc] False -> ["Odd", ..acc] diff --git a/test/gleam/map_test.gleam b/test/gleam/map_test.gleam index fc90b93..95ed66b 100644 --- a/test/gleam/map_test.gleam +++ b/test/gleam/map_test.gleam @@ -162,13 +162,13 @@ pub fn update_test() { pub fn fold_test() { let dict = map.from_list([#("a", 0), #("b", 1), #("c", 2), #("d", 3)]) - let add = fn(_, v, acc) { v + acc } + let add = fn(acc, _, v) { v + acc } dict |> map.fold(0, add) |> should.equal(6) - let concat = fn(k, _, acc) { string.append(acc, k) } + let concat = fn(acc, k, _) { string.append(acc, k) } dict |> map.fold("", concat) |