What are some useful or interesting infinite generators? [closed]
Asked Answered
S

3

0

What are some clever uses for infinite generators? I've seen lots of seemingly trivial examples like "list all even numbers", but I assume there must be others that have more applicability to real-world scenarios. Concrete examples (in any language that support generators) appreciated!

I'll give a trivial sample as an answer.

Sevigny answered 9/2, 2011 at 19:58 Comment(1)
The list of all off-topic questions for stackoverflow.com.Ataman
P
1

Look at the Haskell code on http://rosettacode.org/wiki/Hamming_numbers#Haskell; that uses lazy lists (which are somewhat like generators) in a creative way to list all Hamming numbers.

Pueblo answered 9/2, 2011 at 20:15 Comment(1)
Also has generator version in Python and C#.Dairymaid
L
1

A random generator might be considered clever use.

Lantha answered 9/2, 2011 at 20:9 Comment(0)
P
1

Look at the Haskell code on http://rosettacode.org/wiki/Hamming_numbers#Haskell; that uses lazy lists (which are somewhat like generators) in a creative way to list all Hamming numbers.

Pueblo answered 9/2, 2011 at 20:15 Comment(1)
Also has generator version in Python and C#.Dairymaid
S
0

Trivial example: yield Fibonacci numbers one at a time (sans overflow checking, in C#):

public static IEnumerable<double> Fibonacci()
{
    double n_minus2 = 1;
    double n_minus1 = 1;
    yield return n_minus2;
    yield return n_minus1;

    while(true)
    {
        double n = n_minus2 + n_minus1;
        yield return n;
        n_minus2 = n_minus1;
        n_minus1 = n;
    }
}
Sevigny answered 9/2, 2011 at 20:0 Comment(5)
OP asked for generators "that have more applicability to real-world scenarios".Dairymaid
@delnan - I am OP, and this is the example I said I'd post for illustrative purposes.Sevigny
D'oh! Sorry. But is this kind of thing really what you're looking for? I'd consider this pretty much useless for real problems.Dairymaid
It is. I am wondering if there were structurally similar mechanisms that are actually useful.Sevigny
Why not post your trivial example as part of your original post? 0.oLemmie

© 2022 - 2024 — McMap. All rights reserved.