fork download
  1. function stdout(arg) {
  2. if (this.console) this.console.log(arg)
  3. else if (this.alert) this.alert(arg)
  4. else if (this.print) this.print(arg)
  5. }
  6.  
  7.  
  8. //////////////////////////////////////////////////////////
  9.  
  10.  
  11. Array.prototype.toStreamUsing = function (methods) {
  12.  
  13. var array = this, funcs = [], args = [], neo_methods = {}
  14.  
  15. for (key in methods) {
  16. (function (func) {
  17. neo_methods[key] = function () {
  18. funcs.push(func)
  19. args.push(Array.prototype.slice.apply(arguments))
  20. return neo_methods
  21. }
  22. })(methods[key])
  23. }
  24.  
  25. neo_methods.toArray = function () {
  26. var neo_array = new Array(array.length)
  27. for (var i = 0; i < array.length; ++i) {
  28. var v = array[i]
  29. for (var j = 0; j < args.length; ++j) {
  30. v = funcs[j].apply(null, [v].concat(args[j]))
  31. }
  32. neo_array[i] = v
  33. }
  34. return neo_array
  35. }
  36.  
  37. return neo_methods
  38.  
  39. }
  40.  
  41.  
  42. //////////////////////////////////////////////////////////
  43.  
  44.  
  45. arr = [1,2,3,4,5]
  46.  
  47. methods = {
  48. allPlus : function (n, m) {return n + m},
  49. allMinus: function (n, m) {return n - m}
  50. }
  51.  
  52.  
  53. //////////////////////////////////////////////////////////
  54.  
  55.  
  56. neo_arr = arr.toStreamUsing(methods).allPlus(5).allMinus(3).toArray()
  57. stdout(neo_arr) // 3,4,5,6,7
  58.  
  59.  
  60. stream = arr.toStreamUsing(methods)
  61. stream = stream.allPlus(5)
  62. stream = stream.allMinus(3)
  63. stream.toArray()
  64. stdout(neo_arr) // 3,4,5,6,7
  65.  
Success #stdin #stdout 0.01s 4984KB
stdin
Standard input is empty
stdout
3,4,5,6,7
3,4,5,6,7