aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Schurhammer <julian.schurhammer@gmail.com>2023-12-04 11:49:57 +1300
committerLouis Pilfold <louis@lpil.uk>2023-12-09 09:57:32 +0000
commiteaa795fe6597f8bd9b8b95045f293ff5e81dd96d (patch)
treef149743a18c9121ddef75ea9c100ca2b3f721daa
parente261f0fde8fb3b86830cc0fc5db039378cfb3d9e (diff)
downloadgleam_stdlib-eaa795fe6597f8bd9b8b95045f293ff5e81dd96d.tar.gz
gleam_stdlib-eaa795fe6597f8bd9b8b95045f293ff5e81dd96d.zip
list.index_map: change callback signature
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/list.gleam10
-rw-r--r--test/gleam/list_test.gleam6
3 files changed, 10 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 016099b..ef76982 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,7 @@
# Changelog
+- Changed `list.index_map` callback signature to `fn(a, Int) -> b` from `fn(Int, a) -> b`, to be consistent with `list.index_fold`.
+
## v0.33.1 - 2023-12-02
- Fixed `string.to_graphemes` failing on JavaScript runtimes when the
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index a5cffa9..5f62e58 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -442,14 +442,14 @@ pub fn map_fold(
fn do_index_map(
list: List(a),
- fun: fn(Int, a) -> b,
+ fun: fn(a, Int) -> b,
index: Int,
acc: List(b),
) -> List(b) {
case list {
[] -> reverse(acc)
[x, ..xs] -> {
- let acc = [fun(index, x), ..acc]
+ let acc = [fun(x, index), ..acc]
do_index_map(xs, fun, index + 1, acc)
}
}
@@ -464,11 +464,11 @@ fn do_index_map(
/// ## Examples
///
/// ```gleam
-/// > index_map(["a", "b"], fn(i, x) { #(i, x) })
+/// > index_map(["a", "b"], fn(x, i) { #(i, x) })
/// [#(0, "a"), #(1, "b")]
/// ```
///
-pub fn index_map(list: List(a), with fun: fn(Int, a) -> b) -> List(b) {
+pub fn index_map(list: List(a), with fun: fn(a, Int) -> b) -> List(b) {
do_index_map(list, fun, 0, [])
}
@@ -1718,7 +1718,7 @@ pub fn permutations(l: List(a)) -> List(List(a)) {
[] -> [[]]
_ ->
l
- |> index_map(fn(i_idx, i) {
+ |> index_map(fn(i, i_idx) {
l
|> index_fold(
[],
diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam
index 0bebe2f..a89add7 100644
--- a/test/gleam/list_test.gleam
+++ b/test/gleam/list_test.gleam
@@ -697,14 +697,14 @@ pub fn sort_test() {
}
pub fn index_map_test() {
- list.index_map([3, 4, 5], fn(i, x) { #(i, x) })
+ list.index_map([3, 4, 5], fn(x, i) { #(i, x) })
|> should.equal([#(0, 3), #(1, 4), #(2, 5)])
- let f = fn(i, x) { #(x, i) }
+ let f = fn(x, i) { #(x, i) }
list.index_map(["a", "b"], f)
|> should.equal([#("a", 0), #("b", 1)])
- let f = fn(i, x) { #(x, i) }
+ let f = fn(x, i) { #(x, i) }
list.index_map(["a", "b", "c"], f)
|> should.equal([#("a", 0), #("b", 1), #("c", 2)])
}