Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     Enumerable.prototype.where = function(predicate) {     
  2.         var e = this.getEnumerator();
  3.        
  4.         return new Enumerable(function() {
  5.             while(true) {
  6.                 if(!e.next())
  7.                     return { finished: true };
  8.                
  9.                 if(predicate(e.current()))
  10.                     return { finished: false, val: e.current() };
  11.             }
  12.         });
  13.     };
  14.    
  15.     Enumerable.prototype.select = function(selector) {
  16.         var e = this.getEnumerator();
  17.        
  18.         return new Enumerable(function() {
  19.             if(!e.next())
  20.                 return { finished: true };
  21.            
  22.             return { finished: false, val: selector(e.current()) };
  23.         });
  24.     };
  25.    
  26.     Enumerable.prototype.each = function(fn) {
  27.         var e = this.getEnumerator();
  28.        
  29.         while(e.next())
  30.             fn(e.current());
  31.     };
  32.    
  33.     Enumerable.prototype.reverse = function() {
  34.         var stack = [];
  35.         this.each(function(x) { stack.unshift(x); });
  36.        
  37.         return new Enumerable(stack);
  38.     };
  39.    
  40.     Enumerable.prototype.at = function(index) {
  41.         var e = this.getEnumerator();
  42.         var i = -1;
  43.        
  44.         while(e.next())
  45.         {          
  46.             i++;
  47.            
  48.             if(i == index)
  49.                 return e.current();
  50.         }
  51.        
  52.         return null;
  53.     };
  54.    
  55.     Enumerable.prototype.len = function() {
  56.         var e = this.getEnumerator();
  57.         var len = 0;
  58.        
  59.         while(e.next())
  60.             len++;
  61.            
  62.         return len;
  63.     };
  64.    
  65.     Enumerable.prototype.array = function() {
  66.         var a = [];
  67.         var e = this.getEnumerator();
  68.        
  69.         while(e.next())
  70.             a.push(e.current());
  71.            
  72.         return a;
  73.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement