Skip to content

Instantly share code, notes, and snippets.

@mhingston
Created April 7, 2016 09:21
Show Gist options
  • Save mhingston/c345cbef3cf11f49b707a93abe0c4235 to your computer and use it in GitHub Desktop.
Save mhingston/c345cbef3cf11f49b707a93abe0c4235 to your computer and use it in GitHub Desktop.
"use strict"
// Iterator with generator
let fibonacci =
{
[Symbol.iterator]: function*()
{
let pre = 0, cur = 1
while(true)
{
let temp = pre
pre = cur
cur += temp
yield cur
}
}
}
for(let n of fibonacci)
{
if(n > 1000)
{
break
}
console.log(n)
}
// Generator function
let fibonacci = function*()
{
let pre = 0, cur = 1
while(true)
{
let temp = pre
pre = cur
cur += temp
yield cur
}
}
let temp = fibonacci()
while(true)
{
let next = temp.next()
if(next.done || next.value > 1000)
{
break
}
else
{
console.log(next.value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment