function stdout(arg) {
if (this.console) this.console.log(arg)
else if (this.alert) this.alert(arg)
else if (this.print) this.print(arg)
}
//////////////////////////////////////////////////////////
Array.prototype.toStreamUsing = function (methods) {
var array = this, funcs = [], args = [], neo_methods = {}
for (key in methods) {
(function (func) {
neo_methods[key] = function () {
funcs.push(func)
args.push(Array.prototype.slice.apply(arguments))
return neo_methods
}
})(methods[key])
}
neo_methods.toArray = function () {
var neo_array = new Array(array.length)
for (var i = 0; i < array.length; ++i) {
var v = array[i]
for (var j = 0; j < args.length; ++j) {
v = funcs[j].apply(null, [v].concat(args[j]))
}
neo_array[i] = v
}
return neo_array
}
return neo_methods
}
//////////////////////////////////////////////////////////
arr = [1,2,3,4,5]
methods = {
allPlus : function (n, m) {return n + m},
allMinus: function (n, m) {return n - m}
}
//////////////////////////////////////////////////////////
neo_arr = arr.toStreamUsing(methods).allPlus(5).allMinus(3).toArray()
stdout(neo_arr) // 3,4,5,6,7
stream = arr.toStreamUsing(methods)
stream = stream.allPlus(5)
stream = stream.allMinus(3)
stream.toArray()
stdout(neo_arr) // 3,4,5,6,7
ZnVuY3Rpb24gc3Rkb3V0KGFyZykgewogICAgaWYgICAgKHRoaXMuY29uc29sZSkgdGhpcy5jb25zb2xlLmxvZyhhcmcpCiAgICBlbHNlIGlmICh0aGlzLmFsZXJ0KSB0aGlzLmFsZXJ0KGFyZykKICAgIGVsc2UgaWYgKHRoaXMucHJpbnQpIHRoaXMucHJpbnQoYXJnKQp9IAoKCi8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8KCgpBcnJheS5wcm90b3R5cGUudG9TdHJlYW1Vc2luZyA9IGZ1bmN0aW9uIChtZXRob2RzKSB7CgogICAgdmFyIGFycmF5ID0gdGhpcywgZnVuY3MgPSBbXSwgYXJncyA9IFtdLCBuZW9fbWV0aG9kcyA9IHt9CgogICAgZm9yIChrZXkgaW4gbWV0aG9kcykgewogICAgICAgIChmdW5jdGlvbiAoZnVuYykgewogICAgICAgICAgICBuZW9fbWV0aG9kc1trZXldID0gZnVuY3Rpb24gKCkgewogICAgICAgICAgICAgICAgZnVuY3MucHVzaChmdW5jKQogICAgICAgICAgICAgICAgYXJncy5wdXNoKEFycmF5LnByb3RvdHlwZS5zbGljZS5hcHBseShhcmd1bWVudHMpKQogICAgICAgICAgICAgICAgcmV0dXJuIG5lb19tZXRob2RzCiAgICAgICAgICAgIH0KICAgICAgICB9KShtZXRob2RzW2tleV0pCiAgICB9CiAgICAKICAgIG5lb19tZXRob2RzLnRvQXJyYXkgPSBmdW5jdGlvbiAoKSB7CiAgICAgICAgdmFyIG5lb19hcnJheSA9IG5ldyBBcnJheShhcnJheS5sZW5ndGgpCiAgICAgICAgZm9yICh2YXIgaSA9IDA7IGkgPCBhcnJheS5sZW5ndGg7ICsraSkgewogICAgICAgICAgICB2YXIgdiA9IGFycmF5W2ldCiAgICAgICAgICAgIGZvciAodmFyIGogPSAwOyBqIDwgYXJncy5sZW5ndGg7ICsraikgewogICAgICAgICAgICAgICAgdiA9IGZ1bmNzW2pdLmFwcGx5KG51bGwsIFt2XS5jb25jYXQoYXJnc1tqXSkpCiAgICAgICAgICAgIH0KICAgICAgICAgICAgbmVvX2FycmF5W2ldID0gdgogICAgICAgIH0KICAgICAgICByZXR1cm4gbmVvX2FycmF5CiAgICB9CiAgICAKICAgIHJldHVybiBuZW9fbWV0aG9kcwogICAgCn0KCgovLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vCgoKYXJyID0gWzEsMiwzLDQsNV0KCm1ldGhvZHMgPSB7CiAgICBhbGxQbHVzIDogZnVuY3Rpb24gKG4sIG0pIHtyZXR1cm4gbiArIG19LAogICAgYWxsTWludXM6IGZ1bmN0aW9uIChuLCBtKSB7cmV0dXJuIG4gLSBtfQp9CgoKLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8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