Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
1 change: 1 addition & 0 deletions 1 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### API changes

- Add `Result.forEach` https://github.com/rescript-association/rescript-core/pull/116
- Add missing Array functions : `indexOfFromOpt`, `findLast`, `findLastIndex`, `findLastIndexOpt`, `findLastIndexWithIndex`, `findLastIndexWithIndexOpt` https://github.com/rescript-association/rescript-core/pull/126/files

## 0.2.0

Expand Down
27 changes: 27 additions & 0 deletions 27 src/Core__Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,36 @@ function findMap(arr, f) {
};
}

function findLastIndexOpt(xs, pred) {
var inx = xs.findLastIndex(pred);
if (inx !== -1) {
return inx;
}

}

function findLastIndexWithIndexOpt(xs, pred) {
var inx = xs.findLastIndex(pred);
if (inx !== -1) {
return inx;
}

}

function indexOfFromOpt(xs, val, fromInx) {
var inx = xs.indexOf(val, fromInx);
if (inx !== -1) {
return inx;
}

}

export {
make ,
fromInitializer ,
sort ,
indexOfOpt ,
indexOfFromOpt ,
lastIndexOfOpt ,
reduce ,
reduceWithIndex ,
Expand All @@ -155,5 +180,7 @@ export {
shuffle ,
shuffleInPlace ,
findMap ,
findLastIndexOpt ,
findLastIndexWithIndexOpt ,
}
/* No side effect */
27 changes: 27 additions & 0 deletions 27 src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,30 @@ let findMap = (arr, f) => {
}

@send external at: (array<'a>, int) => option<'a> = "at"

@send
external findLast: (array<'a>, 'a => bool) => option<'a> = "findLast"

@send
external findLastIndex: (array<'a>, 'a => bool) => int = "findLastIndex"

let findLastIndexOpt = (xs, pred) =>
switch findLastIndex(xs, pred) {
| -1 => None
| inx => Some(inx)
}

@send
external findLastIndexWithIndex: (array<'a>, ('a, int) => bool) => int = "findLastIndex"

let findLastIndexWithIndexOpt = (xs, pred) =>
switch findLastIndexWithIndex(xs, pred) {
| -1 => None
| inx => Some(inx)
}

let indexOfFromOpt = (xs, val, fromInx) =>
switch indexOfFrom(xs, val, fromInx) {
| -1 => None
| inx => Some(inx)
}
129 changes: 127 additions & 2 deletions 129 src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,11 @@ Console.log([{"language": "ReScript"}]->Array.indexOfOpt({"language": "ReScript"
```
*/
let indexOfOpt: (array<'a>, 'a) => option<int>

@send external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf"

let indexOfFromOpt: (array<'a>, 'a, int) => option<int>

/**
`joinWith(array, separator)` produces a string where all items of `array` are printed, separated by `separator`. Under the hood this will run JavaScript's `toString` on all the array items.

Expand All @@ -383,9 +386,53 @@ Console.log(array->Array.joinWith(" -- ")) // 1 -- 2 -- 3
*/
@send
external joinWith: (array<'a>, string) => string = "join"
@send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf"

/**
`lastIndexOf(array, item)` returns the last index of `item` in `array`, or `-1` if it is not present. **Note:** [Strict equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) is used when searching, which may produce unexpected results; use `Array.findLastIndexOf` to search by value.

See [Array.lastIndexOf on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf).

## Examples
```rescript
[3, 5, 7, 5, 8]->Array.lastIndexOf(5) // 3
[3, 5, 7, 5, 8]->Array.lastIndexOf(99) // -1
[{"color": "red"}]->Array.lastIndexOf({"color": "red"}) // -1, because of strict equality
```
*/
@send
external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf"

/**
`lastIndexOfOpt(array, item)` returns the last index of `item` in `array`, or `None` if it is not present. **Note:** [Strict equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) is used when searching, which may produce unexpected results; use `Array.findLastIndexOf` to search by value.

See [Array.lastIndexOf on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf).

## Examples
```rescript
[3, 5, 7, 5, 8]->Array.lastIndexOf(5) // Some(3)
[3, 5, 7, 5, 8]->Array.lastIndexOf(99) // None
[{"color": "red"}]->Array.lastIndexOf({"color": "red"}) // None, because of strict equality
```
*/
let lastIndexOfOpt: (array<'a>, 'a) => option<int>
@send external lastIndexOfFrom: (array<'a>, 'a, int) => int = "lastIndexOf"

/**
`lastIndexOfFrom(array, item, fromIndex)` returns the last index of `item` in `array`, or `-1` if it is not present. The search begins at `fromIndex`. A negative index counts back from the end of the array.

**Note:** [Strict equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) is used when searching, which may produce unexpected results; use `Array.findLastIndexOf` to search by value.

See [Array.lastIndexOf on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf).

## Examples
```rescript
[3, 5, 7, 5, 8]->Array.lastIndexOfFrom(5, -3) // 1
[3, 5, 7, 5, 8]->Array.lastIndexOfFrom(5, 4) // 3
[3, 5, 7, 5, 8]->Array.lastIndexOfFrom(5, 0) // -1
[{"color": "red"}]->Array.lastIndexOfFrom({"color": "red"}, 99) // -1, because of strict equality
```
*/
@send
external lastIndexOfFrom: (array<'a>, 'a, int) => int = "lastIndexOf"

/**
`slice(array, ~start, ~end)` creates a new array of items copied from `array` from `start` until (but not including) `end`.
Expand Down Expand Up @@ -955,3 +1002,81 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b>
*/
@send
external at: (array<'a>, int) => option<'a> = "at"

/**
`findLast(array, predicate)` iterates the array in **reverse** order and returns the value of the first element that satisfies the provided testing function. Returns `None` if no elements satisfy the predicate.

See [Array.findLast on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast)

## Examples
```rescript
let nums = [3, 5, 7, 5, 8]
nums->Array.findLast(i => i < 7) // Some(5)
nums->Array.findLast(i=> i > 100) // None
[]->Array.findLast(_ => true) // None
```
*/
@send
external findLast: (array<'a>, 'a => bool) => option<'a> = "findLast"

/**
`findLastIndex(array, predicate)` iterates the array in **reverse** order and returns the index of the first element that satisfies the provided testing function. Returns `-1` if no elements satisfy the predicate.

See [Array.findLastIndex on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex)

## Examples
```rescript
let nums = [3, 5, 7, 5, 8]
nums->Array.findLastIndex(i => i < 7) // 3
nums->Array.findLast(i => i > 100) // -1
[]->Array.findLast(_ => true) // -1
```
*/
@send
external findLastIndex: (array<'a>, 'a => bool) => int = "findLastIndex"

/**
`findLastIndexOpt(array, predicate)` iterates the array in **reverse** order and returns the index of the first element that satisfies the provided testing function. Returns `None` if no elements satisfy the predicate.

See [Array.findLastIndex on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex)

## Examples
```rescript
let nums = [3, 5, 7, 5, 8]
nums->Array.findLastIndex(i => i < 7) // Some(3)
nums->Array.findLast(i => i > 100) // None
[]->Array.findLast(_ => true) // None
```
*/
let findLastIndexOpt: (array<'a>, 'a => bool) => option<int>

/**
`findLastIndexWithIndex(array, predicate)` iterates the array in **reverse** order and returns the index of the first element that satisfies the callback `predicate`. The callback receives the current element being processed followed by its index. Returns `-1` if no elements satisfy the predicate.

See [Array.findLastIndex on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex)

## Examples
```rescript
let nums = [3, 5, 7, 5, 8]
nums->Array.findLastIndexWithIndex((val, inx) => val == 5 && inx <= 2) // 1
nums->Array.findLastIndexWithIndex((val, inx) => val == 5 && inx >= 1) // 3
nums->Array.findLastIndexWithIndex((val, inx) => val >= 8 && inx < 2) // -1
```
*/
@send
external findLastIndexWithIndex: (array<'a>, ('a, int) => bool) => int = "findLastIndex"

/**
`findLastIndexWithIndexOpt(array, predicate)` iterates the array in **reverse** order and returns the index of the first element that satisfies the callback `predicate`. The callback receives the current element being processed followed by its index. Returns `None` if no elements satisfy the predicate.

See [Array.findLastIndex on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex)

## Examples
```rescript
let nums = [3, 5, 7, 5, 8]
nums->Array.findLastIndexWithIndex((val, inx) => val == 5 && inx <= 2) // 1
nums->Array.findLastIndexWithIndex((val, inx) => val == 5 && inx >= 1) // 3
nums->Array.findLastIndexWithIndex((val, inx) => val >= 8 && inx < 2) // None
```
*/
let findLastIndexWithIndexOpt: (array<'a>, ('a, int) => bool) => option<int>
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.