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 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..d9add930 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 its 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"