fork download
  1. Object.defineProperty(Array.prototype, 'toStreamUsing', {
  2. configurable: true,
  3. value: function (methods) {
  4.  
  5. let array = this, funcs = [x => x], args = []
  6.  
  7. function P(...arg) {
  8. return new Proxy({}, {
  9. get: (r, key) => {
  10. funcs.push(methods[key])
  11. args.push(arg)
  12. return key=='toArray' ? A : P
  13. }
  14. })
  15. }
  16.  
  17. function A() {
  18. return array.map(
  19. v => args.reduce(
  20. (v, arg, i) => funcs[i].apply(null ,[v, ...arg]), v
  21. )
  22. )
  23. }
  24.  
  25. return P()
  26.  
  27. }
  28. })
  29.  
  30.  
  31. //////////////////////////////////////////////////////////
  32.  
  33.  
  34. arr = [1,2,3,4,5]
  35.  
  36. methods = {
  37. allPlus : (n, m) => n + m ,
  38. allMinus: (n, m) => n - m
  39. }
  40.  
  41.  
  42. //////////////////////////////////////////////////////////
  43.  
  44.  
  45. arr.toStreamUsing(methods).allPlus(5).allMinus(3).toArray()
  46. // 3,4,5,6,7
  47.  
  48.  
  49. stream = arr.toStreamUsing(methods)
  50. stream = stream.allPlus(5)
  51. stream = stream.allMinus(3)
  52. stream.toArray()
  53. // 3,4,5,6,7
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty