From 2c78659b3c0e8bc0965f544e7b69dcc0c402e38a Mon Sep 17 00:00:00 2001 From: dkirchhof Date: Sat, 18 Feb 2023 11:06:24 +0100 Subject: [PATCH 1/3] Add array.at function --- src/Core__Array.res | 2 ++ src/Core__Array.resi | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Core__Array.res b/src/Core__Array.res index db444b69..c268bf71 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -196,3 +196,5 @@ let filterMap = (a, f) => filterMapU(a, (. a) => f(a)) // TODO: Change this implementation? let flatMap = (a, f) => []->concatMany(map(a, f)) + +@send external at: (array<'a>, int) => option<'a> = "at" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 87daca06..6cc3a797 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -103,3 +103,21 @@ let filterMap: (array<'a>, 'a => option<'b>) => array<'b> let shuffle: array<'a> => array<'a> let shuffleInPlace: array<'a> => unit let flatMap: (array<'a>, 'a => array<'b>) => array<'b> + + +/** + `at(array, index)` + + Get an element by it's index. Negative indices count backwards from the last item. + + ## Examples + ```rescript + ["a", "b", "c"]->Array.at(0) // Some("a") + ["a", "b", "c"]->Array.at(2) // Some("c") + ["a", "b", "c"]->Array.at(3) // None + ["a", "b", "c"]->Array.at(-1) // Some("c") + ["a", "b", "c"]->Array.at(-3) // Some("a") + ["a", "b", "c"]->Array.at(-4) // None + ``` +*/ +@send external at: (array<'a>, int) => option<'a> = "at" From b29c932b1270a6b039386abbe451815611bc9659 Mon Sep 17 00:00:00 2001 From: Daniel Kirchhof Date: Sat, 18 Feb 2023 12:05:57 +0100 Subject: [PATCH 2/3] Fix wording Co-authored-by: Glenn Slotte --- src/Core__Array.resi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 6cc3a797..d9add930 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -108,7 +108,7 @@ let flatMap: (array<'a>, 'a => array<'b>) => array<'b> /** `at(array, index)` - Get an element by it's index. Negative indices count backwards from the last item. + Get an element by its index. Negative indices count backwards from the last item. ## Examples ```rescript From 1ae8880a7ca1777cbb62bc511c52983ca65e62c8 Mon Sep 17 00:00:00 2001 From: dkirchhof Date: Sat, 18 Feb 2023 14:00:04 +0100 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 073e8b4b..ee0bd5a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Change `Set.add` to not return self, to indicate that it's mutable. https://github.com/rescript-association/rescript-core/pull/35 - Change `Iterator` bindings to have the same shape as `AsyncIterator` for consistency. https://github.com/rescript-association/rescript-core/pull/34 - Add `Iterator.toArray` binding for turning an iterator into an array. https://github.com/rescript-association/rescript-core/pull/34 +- Add `Array.at` binding for returning an array item by its index. https://github.com/rescript-association/rescript-core/pull/48 ### Documentation