Created
May 13, 2014 12:48
-
-
Save Balamir/8e057870f045b3c30772 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.toIterator = function*() { | |
for (var i = 0, l = this.length; i < l; i++) { | |
yield this[i] | |
} | |
} | |
Object.prototype.map = function*(lambda) { | |
for (var value of this) { | |
yield lambda(value) | |
} | |
} | |
Object.prototype.take = function*(n) { | |
for (var i = 0; i < n; i++) { | |
var value = this.next().value | |
if (typeof value == 'undefined') break | |
yield value | |
} | |
} | |
Object.prototype.toArray = function(n) { | |
var list = [] | |
for (var item of this) { | |
list.push(item) | |
} | |
return list | |
} | |
var array = [0, 1, 2, 3, 4, 5, 6, 7] | |
var addOne = function(v) { | |
return v + 1 | |
} | |
var result = array.toIterator().map(addOne).take(3).toArray() | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment