Platinum Partner

JavaScript Versions Of The Functions Described In Martin Fowler's Article "CollectionClosureMethod"

See the original article at http://www.martinfowler.com/bliki/CollectionClosureMethod.html.

These implement all the methods except the sorting related ones.

Array.prototype.select = function(detect) {
    var result = [];
    for (var i = 0; i < this.length; ++i) {
        if (detect(this[i])) {
            result.push(this[i]);
        }
    }
    return result;
};

Array.prototype.reject = function(detect) {
    var result = [];
    for (var i = 0; i < this.length; ++i) {
        if (!detect(this[i])) {
            result.push(this[i]);
        }
    }
    return result;
};

Array.prototype.partition = function(detect) {
    var yeses = [];
    var nos   = [];
    for (var i = 0; i < this.length; ++i) {
        if (detect(this[i])) {
            yeses.push(this[i]);
        } else {
            nos.push(this[i]);
        }
    }
    return [yeses, nos];
};

Array.prototype.all = function(detect) {
    for (var i = 0; i < this.length; ++i) {
        if (!detect(this[i])) {
            return false;
        }
    }
    return true;
};

Array.prototype.any = function(detect) {
    for (var i = 0; i < this.length; ++i) {
        if (detect(this[i])) {
            return true;
        }
    }
    return false;
};

Array.prototype.find = function(detect, ifNone) {
    for (var i = 0; i < this.length; ++i) {
        if (detect(this[i])) {
            return this[i];
        }
    }

    if (ifNone === null) {
        return null;
    }

    return ifNone();
};

Array.prototype.map = function(mapper) {
    var result = [];
    for (var i = 0; i < this.length; ++i) {
        result.push(mapper(this[i]));
    }
    return result;
};

Array.prototype.inject = function(initial, reducer) {
    var result = initial;
    for (var i = 0; i < this.length; ++i) {
        result = reducer(result, this[i]);
    }
    return result;
};
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}