Skip to content

Commit a698fdc

Browse files
authored
feat(stdlib)!: Use Array length as default end in Array.slice function (#1762)
* feat(stdlib)!: Use Array length as default end in `Array.slice` function * chore: run formatter Weird that the formatter didnt format on save.
1 parent 78a81d4 commit a698fdc

File tree

3 files changed

+65
-48
lines changed

3 files changed

+65
-48
lines changed

compiler/test/stdlib/array.test.gr

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -347,19 +347,20 @@ assert Array.join(", ", [>]) == ""
347347
// Array.slice
348348
let testChars = [> 'a', 'b', 'c']
349349

350-
assert Array.slice(0, 1, testChars) == [> 'a']
351-
assert Array.slice(1, Array.length(testChars), testChars) == [> 'b', 'c']
352-
assert Array.slice(0, 0, testChars) == [>]
350+
assert Array.slice(0, end=1, testChars) == [> 'a']
351+
assert Array.slice(1, end=Array.length(testChars), testChars) == [> 'b', 'c']
352+
assert Array.slice(1, testChars) == [> 'b', 'c']
353+
assert Array.slice(0, end=0, testChars) == [>]
353354
// Purposefully huge number
354-
assert Array.slice(1, 10000, testChars) == [> 'b', 'c']
355+
assert Array.slice(1, end=10000, testChars) == [> 'b', 'c']
355356
// Negative indexing
356-
assert Array.slice(1, -1, testChars) == [> 'b']
357-
assert Array.slice(-2, -1, testChars) == [> 'b']
357+
assert Array.slice(1, end=-1, testChars) == [> 'b']
358+
assert Array.slice(-2, end=-1, testChars) == [> 'b']
358359
// Bad order
359-
assert Array.slice(2, 1, testChars) == [>]
360-
assert Array.slice(-1, -2, testChars) == [>]
360+
assert Array.slice(2, end=1, testChars) == [>]
361+
assert Array.slice(-1, end=-2, testChars) == [>]
361362
// Empty
362-
assert Array.slice(1, 4, [>]) == [>]
363+
assert Array.slice(1, end=4, [>]) == [>]
363364

364365
// Array.sort
365366
// Numbers
@@ -780,20 +781,21 @@ module Immutable {
780781
// Array.slice
781782
let testChars = fromList(['a', 'b', 'c'])
782783

783-
assert Array.slice(0, 1, testChars) == fromList(['a'])
784-
assert Array.slice(1, Array.length(testChars), testChars) ==
784+
assert Array.slice(0, end=1, testChars) == fromList(['a'])
785+
assert Array.slice(1, end=Array.length(testChars), testChars) ==
785786
fromList(['b', 'c'])
786-
assert Array.slice(0, 0, testChars) == fromList([])
787+
assert Array.slice(1, testChars) == fromList(['b', 'c'])
788+
assert Array.slice(0, end=0, testChars) == fromList([])
787789
// Purposefully huge number
788-
assert Array.slice(1, 10000, testChars) == fromList(['b', 'c'])
790+
assert Array.slice(1, end=10000, testChars) == fromList(['b', 'c'])
789791
// Negative indexing
790-
assert Array.slice(1, -1, testChars) == fromList(['b'])
791-
assert Array.slice(-2, -1, testChars) == fromList(['b'])
792+
assert Array.slice(1, end=-1, testChars) == fromList(['b'])
793+
assert Array.slice(-2, end=-1, testChars) == fromList(['b'])
792794
// Bad order
793-
assert Array.slice(2, 1, testChars) == fromList([])
794-
assert Array.slice(-1, -2, testChars) == fromList([])
795+
assert Array.slice(2, end=1, testChars) == fromList([])
796+
assert Array.slice(-1, end=-2, testChars) == fromList([])
795797
// Empty
796-
assert Array.slice(1, 4, fromList([])) == fromList([])
798+
assert Array.slice(1, end=4, fromList([])) == fromList([])
797799

798800
// Array.sort
799801
// Numbers

stdlib/array.gr

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -904,17 +904,18 @@ let wrapNegativeIndex = (arrLen, idx) => {
904904
* If either index is a negative number, it will be treated as a reverse index from
905905
* the end of the array. e.g. `slice(1, -1, [> 'a', 'b', 'c']) == [> 'b']`.
906906
*
907-
* @param startIndex: The index of the array where the slice will begin (inclusive)
908-
* @param endIndex: The index of the array where the slice will end (exclusive)
907+
* @param start: The index of the array where the slice will begin (inclusive)
908+
* @param end: The index of the array where the slice will end (exclusive)
909909
* @param array: The array to be sliced
910910
* @returns The subset of the array that was sliced
911911
*
912912
* @since v0.4.0
913+
* @history v0.6.0: Default `end` to the Array length
913914
*/
914-
provide let slice = (startIndex, endIndex, array) => {
915+
provide let slice = (start, end=length(array), array) => {
915916
let arrayLength = length(array)
916-
let startIndex = wrapNegativeIndex(arrayLength, startIndex)
917-
let endIndex = wrapNegativeIndex(arrayLength, endIndex)
917+
let startIndex = wrapNegativeIndex(arrayLength, start)
918+
let endIndex = wrapNegativeIndex(arrayLength, end)
918919
// Ensure we aren't working with an `end` value that is too big
919920
let endIndex = if (endIndex > arrayLength) {
920921
arrayLength
@@ -1016,7 +1017,7 @@ provide let rotate = (n, arr) => {
10161017

10171018
/**
10181019
* An immutable array implementation.
1019-
*
1020+
*
10201021
* @since v0.6.0
10211022
* @history v0.5.4: Originally in `"immutablearray"` module
10221023
*/
@@ -1103,7 +1104,7 @@ provide module Immutable {
11031104

11041105
/**
11051106
* An empty array.
1106-
*
1107+
*
11071108
* @since v0.6.0
11081109
* @history v0.5.4: Originally in `"immutablearray"` module
11091110
*/
@@ -1115,7 +1116,7 @@ provide module Immutable {
11151116

11161117
/**
11171118
* Determines if the array contains no elements.
1118-
*
1119+
*
11191120
* @param array: The array to check
11201121
* @returns `true` if the array is empty and `false` otherwise
11211122
*
@@ -1161,7 +1162,7 @@ provide module Immutable {
11611162

11621163
let numToAppend = Number.min(len2, branchingFactor - len1)
11631164
let toAppend = if (numToAppend < len2) {
1164-
mutSlice(0, numToAppend, array2)
1165+
mutSlice(0, end=numToAppend, array2)
11651166
} else {
11661167
array2
11671168
}
@@ -1188,7 +1189,7 @@ provide module Immutable {
11881189
* @param array: The array to access
11891190
* @returns The element from the array
11901191
* @throws IndexOutOfBounds: When the index being accessed is outside the array's bounds
1191-
*
1192+
*
11921193
* @example get(1, fromList([1, 2, 3, 4])) == 2
11931194
* @example get(-1, fromList([1, 2, 3, 4])) == 4
11941195
*
@@ -1226,7 +1227,7 @@ provide module Immutable {
12261227
* @param array: The array to update
12271228
* @returns A new array containing the new element at the given index
12281229
* @throws IndexOutOfBounds: When the index being updated is outside the array's bounds
1229-
*
1230+
*
12301231
* @example set(1, 9, fromList([1, 2, 3, 4, 5])) == fromList([1, 9, 3, 4, 5])
12311232
*
12321233
* @since v0.6.0
@@ -1304,7 +1305,11 @@ provide module Immutable {
13041305
let newArray = replaceTail(appended, array)
13051306
if (numNotAppended > 0) {
13061307
let appendLen = mutLength(toAppend)
1307-
let newTail = mutSlice(appendLen - numNotAppended, appendLen, toAppend)
1308+
let newTail = mutSlice(
1309+
appendLen - numNotAppended,
1310+
end=appendLen,
1311+
toAppend
1312+
)
13081313
replaceTail(newTail, newArray)
13091314
} else {
13101315
newArray
@@ -1378,7 +1383,7 @@ provide module Immutable {
13781383
if (numNotAppended >= 0) {
13791384
let appendLen = mutLength(toAppend)
13801385
{
1381-
btail: mutSlice(appendLen - numNotAppended, appendLen, toAppend),
1386+
btail: mutSlice(appendLen - numNotAppended, end=appendLen, toAppend),
13821387
nodes: [Leaf(appended), ...nodes],
13831388
numNodes: numNodes + 1,
13841389
}
@@ -1432,7 +1437,7 @@ provide module Immutable {
14321437
*
14331438
* @param arrays: A list containing all arrays to combine
14341439
* @returns The new array
1435-
*
1440+
*
14361441
* @example concat([fromList([1, 2]), fromList([3, 4]), fromList([5, 6])]) == fromList([1, 2, 3, 4, 5, 6])
14371442
*
14381443
* @since v0.6.0
@@ -1628,7 +1633,7 @@ provide module Immutable {
16281633
* @param fn: The function to be called on each element, where the value returned will be an array that gets appended to the new array
16291634
* @param array: The array to iterate
16301635
* @returns The new array
1631-
*
1636+
*
16321637
* @example flatMap(n => fromList([n, n + 1]), fromList([1, 3, 5])) == fromList([1, 2, 3, 4, 5, 6])
16331638
*
16341639
* @since v0.6.0
@@ -1850,7 +1855,7 @@ provide module Immutable {
18501855
*
18511856
* Calling this function with arrays of different sizes will cause the returned
18521857
* array to have the length of the smaller array.
1853-
*
1858+
*
18541859
* @param array1: The array to provide values for the first tuple element
18551860
* @param array2: The array to provide values for the second tuple element
18561861
* @returns The new array containing indexed pairs of `(a, b)`
@@ -1869,7 +1874,7 @@ provide module Immutable {
18691874
* applying the function to the first elements of each array, the second element
18701875
* will contain the result of applying the function to the second elements of
18711876
* each array, and so on.
1872-
*
1877+
*
18731878
* Calling this function with arrays of different sizes will cause the returned
18741879
* array to have the length of the smaller array.
18751880
*
@@ -1880,7 +1885,7 @@ provide module Immutable {
18801885
*
18811886
* @example zipWith((a, b) => a + b, fromList([1, 2, 3]), fromList([4, 5, 6])) == fromList([5, 7, 9])
18821887
* @example zipWith((a, b) => a * b, fromList([1, 2, 3]), fromList([4, 5])) == fromList([4, 10])
1883-
*
1888+
*
18841889
* @since v0.6.0
18851890
* @history v0.5.4: Originally in `"immutablearray"` module
18861891
*/
@@ -1944,15 +1949,16 @@ provide module Immutable {
19441949
* @param end: The index of the array where the slice will end (exclusive)
19451950
* @param array: The array to be sliced
19461951
* @returns The subset of the array that was sliced
1947-
*
1952+
*
19481953
* @example slice(0, 2, fromList(['a', 'b', 'c'])) == fromList(['a', 'b'])
19491954
* @example slice(1, -1, fromList(['a', 'b', 'c'])) == fromList(['b'])
19501955
*
19511956
* @since v0.6.0
19521957
* @history v0.5.4: Originally in `"immutablearray"` module
1958+
* @history v0.6.0: Default `end` to the Array length
19531959
*/
19541960

1955-
provide let slice = (start, end, array) => {
1961+
provide let slice = (start, end=length(array), array) => {
19561962
let begin = clampIndex(array.length, start)
19571963
let end = clampIndex(array.length, end)
19581964
let mut i = array.length
@@ -1999,8 +2005,8 @@ provide module Immutable {
19992005

20002006
provide let rotate = (n, array) => {
20012007
let sliceI = if (array.length == 0) 0 else n % array.length
2002-
let before = slice(0, sliceI, array)
2003-
let after = slice(sliceI, array.length, array)
2008+
let before = slice(0, end=sliceI, array)
2009+
let after = slice(sliceI, end=array.length, array)
20042010
append(after, before)
20052011
}
20062012
}

stdlib/array.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,13 +1189,20 @@ Returns:
11891189

11901190
### Array.**slice**
11911191

1192-
<details disabled>
1193-
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
1194-
No other changes yet.
1192+
<details>
1193+
<summary>Added in <code>0.4.0</code></summary>
1194+
<table>
1195+
<thead>
1196+
<tr><th>version</th><th>changes</th></tr>
1197+
</thead>
1198+
<tbody>
1199+
<tr><td><code>next</code></td><td>Default `end` to the Array length</td></tr>
1200+
</tbody>
1201+
</table>
11951202
</details>
11961203

11971204
```grain
1198-
slice : (startIndex: Number, endIndex: Number, array: Array<a>) -> Array<a>
1205+
slice : (start: Number, ?end: Number, array: Array<a>) -> Array<a>
11991206
```
12001207

12011208
Slices an array given zero-based start and end indexes. The value
@@ -1208,8 +1215,8 @@ Parameters:
12081215

12091216
|param|type|description|
12101217
|-----|----|-----------|
1211-
|`startIndex`|`Number`|The index of the array where the slice will begin (inclusive)|
1212-
|`endIndex`|`Number`|The index of the array where the slice will end (exclusive)|
1218+
|`start`|`Number`|The index of the array where the slice will begin (inclusive)|
1219+
|`end`|`Option<Number>`|The index of the array where the slice will end (exclusive)|
12131220
|`array`|`Array<a>`|The array to be sliced|
12141221

12151222
Returns:
@@ -2455,13 +2462,15 @@ Returns:
24552462
</thead>
24562463
<tbody>
24572464
<tr><td><code>0.5.4</code></td><td>Originally in `"immutablearray"` module</td></tr>
2465+
<tr><td><code>next</code></td><td>Default `end` to the Array length</td></tr>
24582466
</tbody>
24592467
</table>
24602468
</details>
24612469

24622470
```grain
24632471
slice :
2464-
(start: Number, end: Number, array: ImmutableArray<a>) -> ImmutableArray<a>
2472+
(start: Number, ?end: Number, array: ImmutableArray<a>) ->
2473+
ImmutableArray<a>
24652474
```
24662475

24672476
Slices an array given zero-based start and end indexes. The value
@@ -2475,7 +2484,7 @@ Parameters:
24752484
|param|type|description|
24762485
|-----|----|-----------|
24772486
|`start`|`Number`|The index of the array where the slice will begin (inclusive)|
2478-
|`end`|`Number`|The index of the array where the slice will end (exclusive)|
2487+
|`end`|`Option<Number>`|The index of the array where the slice will end (exclusive)|
24792488
|`array`|`ImmutableArray<a>`|The array to be sliced|
24802489

24812490
Returns:

0 commit comments

Comments
 (0)