Zip arrays
by dimitrs_papadimitriou
HTML
<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>
JavaScript
Array.prototype.matchWith = function (pattern) {
return this.length == 0 ?
pattern.empty() :
pattern.concat(this[0], this.slice(1));// informed decesion not to clone
}
Array.prototype.zip = function (a2) {
return this.matchWith({
empty: () => a2,
concat: (x, xs) => a2.matchWith({
empty: () => this,
concat: (y, ys) => [x, y].concat(xs.zip(ys))
})
});
}
console.log([1, 2, 3].zip([6, 7, 8, 7, 9, 10, 11])); // [1, 6, 2, 7, 3, 8, 7, 9, …]