Nested Iterators, part 1

C# 2.0 introduced a powerful feature called an iterator, a method which returns an IEnumerable<T> or IEnumerator<T> using the new yield keyword.

Using an iterator, you can quickly and easily create a method which returns lazily a sequence of values.  However, lazily returning a sequence of sequences (IEnumerable<IEnumerable<T>>) is not so simple.

The obvious approach is to yield return a List<T>:

IEnumerable<IEnumerable<int>> SemiLazy() {
    for(int i = 0; i < 10; i++) {
        List<int> numbers = new List<int>();
        for(int j = 0; j < 10; j++)
            numbers.Add(i * 10 + j);
            
        yield return numbers;
    }
}

(This can be shortened to a single LINQ statement, but that’s beyond the point of this post: Enumerable.Range(0, 10).Select(i => Enumerable.Range(10 * i, 10)) )

This approach is very simple, but isn’t very lazy; each subsequence will be computed in its entirety, whether it’s consumed or not.

This approach also has a subtle catch: the iterator must return a different List<T> instance every time.

If you “optimize” it to re-use the instance, you’ll break callers which don’t use the subsequences immediately:

IEnumerable<IEnumerable<int>> Wrong() {
    List<int> numbers = new List<int>();
    for(int i = 0; i < 10; i++) {
        numbers.Clear();
        for(int j = 0; j < 10; j++)
            numbers.Add(i * 10 + j);
            
        yield return numbers;
    }
}

Calling SemiLazy().ToArray()[0].First() will return 0 (the first element in the first subsequence); calling Wrong().ToArray()[0].First() will return 90 (since all subsequences refer to the same instance).

Next: How can we achieve full laziness?

9 comments:

I love your publications Mimi

As for me, these helpful advices will be useful for future teachers. Especially if you want to learn more about Montessori education

I surely enjoying every little bit of it. It is a great website and nice share to read. I want to thank you. Good job ! You guys do a great blog and have some great contents. Keep up the good work. I am working in a dissertation writing service .So daily I read this type of articles. Our company wrote so many articles about tourism.

I don't get a word from the thing you wrote here. It looks too complicated to me. Is this one of those isaac newton essay? It looks familiar, but still makes no sens to me.

Bandar togel sidney, togel sdy online terpercaya dan terbaik 2021, hadir di tengah2 masyarakat indonesia dengan minimal deposit 25.000. dapatkan keluaran data sidney, pengeluaran sdy, keluaran sdy, di situs resmi togel sidney asiktogelku. togel online

Asiktogelku selaku agen bandar togel online resmi di indonesia 2021. hadir dengan banyak jenis permainan judi online di antara nya; togel online, slot online, live casino, tembak ikan, dan sportbook. tidak hanya itu saja, agen bandar togel resmi juga menyediakan deposit pulsa tanpa potongan sama sekali. so anda bisa bermain permainan dengan melakukan top up atau deposit menggunakan pulsa.
togel online

Situs bandar togel sidney, togel sdy, togel online terbaik dan terpercaya 2021. dengan 1 user id anda dapat bermain semua jenis permainan yang ada di situs asiktogelku. banyak jenis permainan serta bonus yang di sediakan oleh bandar agen togel online resmi indonesia, bagi para pengemar judi togel indonesia yang baru saja bergabung anda bisa mendapatkan bonus new member 50% dari agen togel sdy resmi indonesia. togel online

ASIKTOGELKU SITUS JUDI TOGEL ONLINE TERBAIK DAN TERPERCAYA MENYEDIAKAN BERBAGAI ANEKA RAGAM PERMAINAN SEPERTI SPORTBOOKS, LIVE CASINO, TEMBAK IKAN, SLOT ONLINE, TOGEL ONLINE DAN MASIH BANYAK PERMAINAN LAIN YANG BISA DIMAINKAN DENGAN SANGAT AMAN YANG JUGA MERUPAKAN SALAH SATU SITUS TOTO ONLINE TERBAIK DAN BISA DIMAINKAN MELALUI PC ATAU KOMPUTER DAN MEMPUNYAI APPLIKASI YANG BISA DI MAINKAN DI HP ANDROID ATAU IOS YANG ANDA MILIKI DAN BERIKUT BEBERAPA PERMAINAN JUDI TOGEL ONLINE. togel online

Post a Comment