Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. var fusion = (function () {
  2. var min = Math.min;
  3.  
  4. Iterator.prototype = {
  5. map: function (f) {
  6. return new MapIterator([f, this.at], this.length);
  7. },
  8. take: take,
  9. toArray: toArray
  10. };
  11.  
  12. MapIterator.prototype = {
  13. map: function (f) {
  14. return new MapIterator([f].concat(this.fs), this.length);
  15. },
  16. take: take,
  17. toArray: toArray
  18. };
  19.  
  20. return iterator;
  21.  
  22. function compose(f, g) {
  23. return function (x) {
  24. return f(g(x));
  25. };
  26. }
  27.  
  28. function iterator(a) {
  29. return new Iterator(function (index) {
  30. return a[index];
  31. }, a.length);
  32. }
  33.  
  34. function Iterator(at, length) {
  35. this.at = at;
  36. this.length = length;
  37. }
  38.  
  39. function MapIterator(fs, length) {
  40. this.fs = fs;
  41. this.at = fs.reduce(compose);
  42. this.length = length;
  43. }
  44.  
  45. function take(n) {
  46. return new Iterator(this.at, min(n, this.length));
  47. }
  48.  
  49. function toArray() {
  50. var at = this.at;
  51. var length = this.length;
  52. var array = new Array(length);
  53. var index = 0;
  54.  
  55. while (index < length) array[index] = at(index++);
  56.  
  57. return array;
  58. }
  59. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement