Skip to content
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

🐛 - Fix binding of Array.get, to make sure it boxes options #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/Core__Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ function reduceRightWithIndex(arr, init, f) {
return arr.reduceRight(f, init);
}

function get(arr, i) {
if (i >= 0 && i < arr.length) {
return Caml_option.some(arr[i]);
}

}

function findIndexOpt(array, finder) {
var index = array.findIndex(finder);
if (index !== -1) {
Expand Down Expand Up @@ -166,7 +173,7 @@ function findMap(arr, f) {
}

function last(a) {
return a[a.length - 1 | 0];
return get(a, a.length - 1 | 0);
}

export {
Expand All @@ -180,6 +187,7 @@ export {
reduceWithIndex ,
reduceRight ,
reduceRightWithIndex ,
get ,
findIndexOpt ,
filterMap ,
keepSome ,
Expand Down
8 changes: 7 additions & 1 deletion src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ let reduceRightWithIndex = (arr, init, f) => reduceRightWithIndex(arr, f, init)
@send external some: (array<'a>, 'a => bool) => bool = "some"
@send external someWithIndex: (array<'a>, ('a, int) => bool) => bool = "some"

@get_index external get: (array<'a>, int) => option<'a> = ""
let get = (arr, i) =>
if i >= 0 && i < length(arr) {
Some(getUnsafe(arr, i))
} else {
None
}

@set_index external set: (array<'a>, int, 'a) => unit = ""

@get_index external getSymbol: (array<'a>, Core__Symbol.t) => option<'b> = ""
Expand Down
3 changes: 1 addition & 2 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,7 @@ array->Array.get(0) == Some("Hello") // true
array->Array.get(3) == None // true
```
*/
@get_index
external get: (array<'a>, int) => option<'a> = ""
let get: (array<'a>, int) => option<'a>

/**
`set(array, index, item)` sets the provided `item` at `index` of `array`.
Expand Down