Generators ========== ```Example from 1 languages: JavaScript function* fibonacci(limit) { let [prev, curr] = [0, 1]; while (!limit || curr <= limit) { yield curr; [prev, curr] = [curr, prev + curr]; } } // bounded by upper limit 10 for (let n of fibonacci(10)) { console.log(n); } // generator without an upper bound limit for (let n of fibonacci()) { console.log(n); if (n > 10000) break; } // manually iterating let fibGen = fibonacci(); console.log(fibGen.next().value); // 1 console.log(fibGen.next().value); // 1 console.log(fibGen.next().value); // 2 console.log(fibGen.next().value); // 3 console.log(fibGen.next().value); // 5 console.log(fibGen.next().value); // 8 // picks up from where you stopped for (let n of fibGen) { console.log(n); if (n > 10000) break; } ``` ```Example from 1 languages: Python https://wiki.python.org/moin/Generators ``` ```Example from 1 languages: Ruby # Generator from an Enumerator object chars = Enumerator.new(['A', 'B', 'C', 'Z']) 4.times { puts chars.next } # Generator from a block count = Enumerator.new do |yielder| i = 0 loop { yielder.yield i += 1 } end 100.times { puts count.next } ``` ```Example from 1 languages: C# // Method that takes an iterable input (possibly an array) // and returns all even numbers. public static IEnumerable<int> GetEven(IEnumerable<int> numbers) { foreach (int i in numbers) { if ((i % 2) == 0) { yield return i; } } } ``` * Languages *with* Generators include JavaScript, Python, Ruby, C# * View all concepts with or missing a *hasGenerators* measurement http://pldb.info/../lists/explorer.html#columns=rank~id~appeared~tags~creators~hasGenerators&searchBuilder=%7B%22criteria%22%3A%5B%7B%22condition%22%3A%22null%22%2C%22data%22%3A%22hasGenerators%22%2C%22origData%22%3A%22hasGenerators%22%2C%22type%22%3A%22num%22%2C%22value%22%3A%5B%5D%7D%5D%2C%22logic%22%3A%22AND%22%7D missing http://pldb.info/../lists/explorer.html#columns=rank~id~appeared~tags~creators~hasGenerators&searchBuilder=%7B%22criteria%22%3A%5B%7B%22condition%22%3A%22!null%22%2C%22data%22%3A%22hasGenerators%22%2C%22origData%22%3A%22hasGenerators%22%2C%22type%22%3A%22num%22%2C%22value%22%3A%5B%5D%7D%5D%2C%22logic%22%3A%22AND%22%7D with * Read more about Generators on the web: 1. https://en.wikipedia.org/wiki/Generator_(computer_programming) 1. Built with Scroll v178.2.3