Skip to content

Add array.at function #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 18 additions & 0 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -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"