Skip to content

Commit b8ae3d6

Browse files
authored
feat(stdlib)!: Support zipping arrays of different sizes (#1402)
closes #1395
1 parent f78587a commit b8ae3d6

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

compiler/test/stdlib/array.test.gr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ let arrB = [> 5, 4, 3, 2, 1]
237237

238238
assert Array.zip(arrA, arrB) == [> (1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]
239239

240+
let arrA = [> 1, 2, 3, 4, 5]
241+
let arrB = [> 5, 4, 3, 2, 1, 6, 7]
242+
243+
assert Array.zip(arrA, arrB) == [> (1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]
244+
245+
let arrA = [> 1, 2, 3, 4, 5, 6, 7]
246+
let arrB = [> 5, 4, 3, 2, 1]
247+
248+
assert Array.zip(arrA, arrB) == [> (1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]
249+
240250
// Array.zipWith
241251

242252
let addFn = (a, b) => a + b

stdlib/array.gr

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -807,16 +807,15 @@ export let unique = array => {
807807
* @throws Failure(String): When the arrays have different sizes
808808
*
809809
* @since v0.4.0
810+
* @history v0.5.3: Support zipping arrays of different sizes
810811
*/
811812
export let zip = (array1: Array<a>, array2: Array<b>) => {
812813
let len = length(array1)
813-
if (len != length(array2)) {
814-
fail "arguments to zip must be same length"
815-
} else {
816-
init(len, n => {
817-
(array1[n], array2[n])
818-
})
819-
}
814+
let len2 = length(array2)
815+
let len = if (len > len2) len2 else len
816+
init(len, n => {
817+
(array1[n], array2[n])
818+
})
820819
}
821820

822821
/**

stdlib/array.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,9 +1000,16 @@ Returns:
10001000

10011001
### Array.**zip**
10021002

1003-
<details disabled>
1004-
<summary tabindex="-1">Added in <code>0.4.0</code></summary>
1005-
No other changes yet.
1003+
<details>
1004+
<summary>Added in <code>0.4.0</code></summary>
1005+
<table>
1006+
<thead>
1007+
<tr><th>version</th><th>changes</th></tr>
1008+
</thead>
1009+
<tbody>
1010+
<tr><td><code>next</code></td><td>Support zipping arrays of different sizes</td></tr>
1011+
</tbody>
1012+
</table>
10061013
</details>
10071014

10081015
```grain

0 commit comments

Comments
 (0)