SimonRichardson / squishy-pants

Semi-serious function programming library.
MIT License
5 stars 0 forks source link

Improve Array method improvements. #9

Open SimonRichardson opened 11 years ago

SimonRichardson commented 11 years ago

Consider adding Seq or ArrayList as a way to extend Arrays with squishy methods.

var seq = Seq([1, 2, 3]);
seq.filterNot(_.isEven);
seq.toArray(); // [1, 3]
SimonRichardson commented 11 years ago

It would be a lot easier if we could use Proxies or __noSuchMethod__.

SimonRichardson commented 11 years ago

https://gist.github.com/SimonRichardson/6267447

SimonRichardson commented 11 years ago

Because it's just a pain atm to try and implement this, we should leave it out until chrome gets Proxy. We could test to make sure that Proxy is available, but again, it's a lot of hit just for syntax sugar.

//
//  ## proxy
//
function proxy(ctor) {
    return function(v) {
        var value = isArray(v) ? v : [].slice.call(arguments);
        return new Proxy(ctor(value), {
            get: function(obj, prop) {
                if (prop in obj) return obj[prop];
                else if(!(prop in squishy)) throw new Error('NoSuchMethod ' + prop + ' on ' + squishy);

                return function() {
                    var args = squishy.map([].slice.call(arguments), function(a) {
                        return squishy.extract(a);
                    });
                    return ctor.create(squishy[prop].apply(null, [squishy.extract(obj)].concat(args)));
                };
            }
        });
    };
}