array lenght by catamorhism

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.cata = function (evaluation) {
        return this.matchWith({
            empty: () => evaluation.empty(),
            concat: (head, tail) => evaluation.concat(head,tail.cata(evaluation))
        })
    }
 

    var lenght = [1, 2, 3, 4, 5, 2].cata({
        empty: () => 0,
        concat: (v, r) => 1 + r
    });
        console.log(lenght);