Skip to content

Commit 71c1124

Browse files
dkirchhofglennsl
andauthored
Add array.at function (#48)
* Add array.at function * Fix wording Co-authored-by: Glenn Slotte <glenn@slotte.net> * Update changelog --------- Co-authored-by: Glenn Slotte <glenn@slotte.net>
1 parent e386832 commit 71c1124

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Change `Set.add` to not return self, to indicate that it's mutable. https://github.com/rescript-association/rescript-core/pull/35
99
- Change `Iterator` bindings to have the same shape as `AsyncIterator` for consistency. https://github.com/rescript-association/rescript-core/pull/34
1010
- Add `Iterator.toArray` binding for turning an iterator into an array. https://github.com/rescript-association/rescript-core/pull/34
11+
- Add `Array.at` binding for returning an array item by its index. https://github.com/rescript-association/rescript-core/pull/48
1112

1213
### Documentation
1314

src/Core__Array.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,5 @@ let filterMap = (a, f) => filterMapU(a, (. a) => f(a))
196196

197197
// TODO: Change this implementation?
198198
let flatMap = (a, f) => []->concatMany(map(a, f))
199+
200+
@send external at: (array<'a>, int) => option<'a> = "at"

src/Core__Array.resi

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,21 @@ let filterMap: (array<'a>, 'a => option<'b>) => array<'b>
103103
let shuffle: array<'a> => array<'a>
104104
let shuffleInPlace: array<'a> => unit
105105
let flatMap: (array<'a>, 'a => array<'b>) => array<'b>
106+
107+
108+
/**
109+
`at(array, index)`
110+
111+
Get an element by its index. Negative indices count backwards from the last item.
112+
113+
## Examples
114+
```rescript
115+
["a", "b", "c"]->Array.at(0) // Some("a")
116+
["a", "b", "c"]->Array.at(2) // Some("c")
117+
["a", "b", "c"]->Array.at(3) // None
118+
["a", "b", "c"]->Array.at(-1) // Some("c")
119+
["a", "b", "c"]->Array.at(-3) // Some("a")
120+
["a", "b", "c"]->Array.at(-4) // None
121+
```
122+
*/
123+
@send external at: (array<'a>, int) => option<'a> = "at"

0 commit comments

Comments
 (0)