Skip to content

Commit bbe56ad

Browse files
authored
feat(stdlib)!: Change array rotation direction (#1552)
Closes #1470
1 parent b8ae3d6 commit bbe56ad

File tree

7 files changed

+40
-26
lines changed

7 files changed

+40
-26
lines changed

compiler/test/stdlib/array.test.gr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,31 +387,31 @@ assert arr1 == [> 1, 2, 3, 4, 5]
387387

388388
let arr2 = [> 1, 2, 3, 4, 5]
389389
Array.rotate(3, arr2)
390-
assert arr2 == [> 3, 4, 5, 1, 2]
390+
assert arr2 == [> 4, 5, 1, 2, 3]
391391

392392
let arr3 = [> 1, 2, 3, 4, 5]
393393
Array.rotate(-4, arr3)
394-
assert arr3 == [> 5, 1, 2, 3, 4]
394+
assert arr3 == [> 2, 3, 4, 5, 1]
395395

396396
let arr4 = [> 1, 2, 3, 4, 5]
397397
Array.rotate(5, arr4)
398398
assert arr4 == [> 1, 2, 3, 4, 5]
399399

400400
let arr5 = [> 1, 2, 3, 4, 5]
401401
Array.rotate(48, arr5)
402-
assert arr5 == [> 3, 4, 5, 1, 2]
402+
assert arr5 == [> 4, 5, 1, 2, 3]
403403

404404
let arr6 = [> 1, 2, 3, 4, 5]
405405
Array.rotate(-54, arr6)
406-
assert arr6 == [> 5, 1, 2, 3, 4]
406+
assert arr6 == [> 2, 3, 4, 5, 1]
407407

408408
let arr7 = [>]: Array<Number>
409409
Array.rotate(1, arr7)
410410
assert arr7 == [>]
411411

412412
let arr8 = [> "a", "b", "c"]
413413
Array.rotate(1, arr8)
414-
assert arr8 == [> "c", "a", "b"]
414+
assert arr8 == [> "b", "c", "a"]
415415

416416
let arr9 = [>]
417417
Array.rotate(1, arr9)

stdlib/array.gr

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -977,17 +977,20 @@ export let sort = (comp, array) => {
977977
}
978978

979979
/**
980-
* Rotates array elements by the specified amount to the right, in place.
980+
* Rotates array elements in place by the specified amount to the left, such
981+
* that the `n`th element becomes the first in the array.
981982
*
982983
* If value is negative, array elements will be rotated by the
983-
* specified amount to the left. See examples.
984+
* specified amount to the right. See examples.
984985
*
985986
* @param n: The number of elements to rotate by
986987
* @param arr: The array to be rotated
987988
*
988-
* @example let array = [> 1, 2, 3, 4, 5]; rotate(2, arr); arr == [> 4, 5, 1, 2, 3]
989-
* @example let array = [> 1, 2, 3, 4, 5]; rotate(-1, arr); arr == [> 2, 3, 4, 5, 1]
989+
* @example let array = [> 1, 2, 3, 4, 5]; rotate(2, arr); arr == [> 3, 4, 5, 1, 2]
990+
* @example let array = [> 1, 2, 3, 4, 5]; rotate(-1, arr); arr == [> 5, 1, 2, 3, 4]
990991
* @since v0.4.5
992+
*
993+
* @history v0.6.0: Behavior changed from right-rotation to left-rotation
991994
*/
992995
export let rotate = (n, arr) => {
993996
let rec gcd = (a, b) => {
@@ -1001,13 +1004,12 @@ export let rotate = (n, arr) => {
10011004
let arrLen = length(arr)
10021005
if (arrLen > 0) {
10031006
let k = n % arrLen
1004-
let mut d = -1
10051007
let mut j = 0
10061008
for (let mut i = 0; i < gcd(arrLen, k); i += 1) {
10071009
j = i
10081010
let temp = arr[i]
10091011
while (true) {
1010-
d = (j - k) % arrLen
1012+
let d = (j + k) % arrLen
10111013
if (d == i) {
10121014
break
10131015
}

stdlib/array.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,19 +1189,27 @@ Parameters:
11891189

11901190
### Array.**rotate**
11911191

1192-
<details disabled>
1193-
<summary tabindex="-1">Added in <code>0.4.5</code></summary>
1194-
No other changes yet.
1192+
<details>
1193+
<summary>Added in <code>0.4.5</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>Behavior changed from right-rotation to left-rotation</td></tr>
1200+
</tbody>
1201+
</table>
11951202
</details>
11961203

11971204
```grain
11981205
rotate : (Number, Array<a>) -> Void
11991206
```
12001207

1201-
Rotates array elements by the specified amount to the right, in place.
1208+
Rotates array elements in place by the specified amount to the left, such
1209+
that the `n`th element becomes the first in the array.
12021210

12031211
If value is negative, array elements will be rotated by the
1204-
specified amount to the left. See examples.
1212+
specified amount to the right. See examples.
12051213

12061214
Parameters:
12071215

@@ -1213,10 +1221,10 @@ Parameters:
12131221
Examples:
12141222

12151223
```grain
1216-
let array = [> 1, 2, 3, 4, 5]; rotate(2, arr); arr == [> 4, 5, 1, 2, 3]
1224+
let array = [> 1, 2, 3, 4, 5]; rotate(2, arr); arr == [> 3, 4, 5, 1, 2]
12171225
```
12181226

12191227
```grain
1220-
let array = [> 1, 2, 3, 4, 5]; rotate(-1, arr); arr == [> 2, 3, 4, 5, 1]
1228+
let array = [> 1, 2, 3, 4, 5]; rotate(-1, arr); arr == [> 5, 1, 2, 3, 4]
12211229
```
12221230

stdlib/immutablearray.gr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,8 @@ export let sort = (comp, array) => {
907907
}
908908

909909
/**
910-
* Rotates array elements by the specified amount to the left.
910+
* Rotates array elements by the specified amount to the left, such that the
911+
* `n`th element is the first in the new array.
911912
*
912913
* If value is negative, array elements will be rotated by the
913914
* specified amount to the right. See examples.

stdlib/immutablearray.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,8 @@ No other changes yet.
10141014
rotate : (Number, ImmutableArray<a>) -> ImmutableArray<a>
10151015
```
10161016

1017-
Rotates array elements by the specified amount to the left.
1017+
Rotates array elements by the specified amount to the left, such that the
1018+
`n`th element is the first in the new array.
10181019

10191020
If value is negative, array elements will be rotated by the
10201021
specified amount to the right. See examples.

stdlib/list.gr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,13 @@ export let part = (count, list) => {
512512
}
513513

514514
/**
515-
* Rotates list elements by the specified amount to the left.
515+
* Rotates list elements by the specified amount to the left, such that `n`th
516+
* element is the first in the new list.
516517
*
517518
* If value is negative, list elements will be rotated by the
518519
* specified amount to the right. See examples.
519520
*
520-
* @param count: The number of elements to rotate by
521+
* @param n: The number of elements to rotate by
521522
* @param list: The list to be rotated
522523
*
523524
* @example List.rotate(2, [1, 2, 3, 4, 5]) // [3, 4, 5, 1, 2]
@@ -527,9 +528,9 @@ export let part = (count, list) => {
527528
*
528529
* @since v0.1.0
529530
*/
530-
export let rotate = (count, list) => {
531+
export let rotate = (n, list) => {
531532
let (beginning, end) =
532-
if (count >= 0) part(count, list) else part(length(list) + count, list)
533+
if (n >= 0) part(n, list) else part(length(list) + n, list)
533534
append(end, beginning)
534535
}
535536

stdlib/list.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,8 @@ No other changes yet.
773773
rotate : (Number, List<a>) -> List<a>
774774
```
775775

776-
Rotates list elements by the specified amount to the left.
776+
Rotates list elements by the specified amount to the left, such that `n`th
777+
element is the first in the new list.
777778

778779
If value is negative, list elements will be rotated by the
779780
specified amount to the right. See examples.
@@ -782,7 +783,7 @@ Parameters:
782783

783784
|param|type|description|
784785
|-----|----|-----------|
785-
|`count`|`Number`|The number of elements to rotate by|
786+
|`n`|`Number`|The number of elements to rotate by|
786787
|`list`|`List<a>`|The list to be rotated|
787788

788789
Throws:

0 commit comments

Comments
 (0)