Created
July 1, 2020 14:37
-
-
Save davidchambers/69b37af3f6d90d2148b3574a71a67cfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//# zip :: Array a -> Array a -> Array a | |
//. | |
//. Zip two arrays, xs and ys, of arbitrary lengths. | |
//. | |
//. let n = Math.min (xs.length, ys.length); | |
//. | |
//. If the first array is longer than the second, the result will | |
//. consist of the first n + 1 elements of the first array and | |
//. all n elements of the second array. Otherwise, the result will | |
//. consist of the first n elements of each array. | |
//. | |
//. > zip ([1, 3, 5]) ([2]) | |
//. [1, 2, 3] | |
//. | |
//. > zip ([1, 3, 5]) ([2, 4]) | |
//. [1, 2, 3, 4, 5] | |
//. | |
//. > zip ([1, 3, 5]) ([2, 4, 6]) | |
//. [1, 2, 3, 4, 5, 6] | |
//. | |
//. > zip ([1, 3, 5]) ([2, 4, 6, 8]) | |
//. [1, 2, 3, 4, 5, 6] | |
const zip = xs => ys => { | |
const result = []; | |
for (let idx = 0; idx < xs.length; idx += 1) { | |
result.push (xs[idx]); | |
if (idx === ys.length) break; | |
result.push (ys[idx]); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment