9. Native Array pattern matching
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.shift(), this) ;
};
//using recursion with the help of the pattern matching on the array
Array.prototype.show = function () {
return this.matchWith({
empty: () => "()",
concat: (value, rest) => `(${value},${rest.show()})`
})
}
var display = [1, 2, 3, 4, 5].show( );
console.log(display)