Skip to content

Instantly share code, notes, and snippets.

@Balamir
Created May 13, 2014 12:48
Show Gist options
  • Save Balamir/8e057870f045b3c30772 to your computer and use it in GitHub Desktop.
Save Balamir/8e057870f045b3c30772 to your computer and use it in GitHub Desktop.
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