Generator (computer programming): Difference between revisions

Content deleted Content added
No edit summary
Line 40:
</ref> [[C Sharp (programming language)|C#]],<ref>
[http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx yield (C# Reference)]
</ref> [[Ruby (programming language)|Ruby]], the upcoming version of ECMAScript (Harmony/ES6) and other languages. In CLU and C#, generators are called ''iterators'', and in Ruby, ''enumerators''.
 
===Lisp===
Line 473:
else:
break
</source>
 
===ECMAScript===
 
ECMAScript 6 (AKA Harmony) will feature generator functions.
 
An infinite Fibonacci sequence can be written using a function generator:
 
<source lang="javascript">
 
function* fibonacci() {
 
let init = 1;
 
yield init;
 
let [prev, curr] = [0, init];
 
while (true) {
 
[prev, curr] = [curr, prev + curr];
 
yield curr;
 
}
 
}
 
var gen = fibonacci();
 
console.log(gen.next().value); // 1
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
console.log(gen.next().value); // 5
console.log(gen.next().value); // 8
 
</source>