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 1 commit
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
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 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"