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?

14 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.

You will feel good connecting with most of our tutors. 24 * 7 service and support is provided to all our users. We help with projects requiring All Assignment Help work. Get My Homework Done



شركة تنظيف في الكويت شركة تنظيف بالكويت
فني صحي فني صحي في الكويت
سباك الكويت سباك بالكويت

شركة تنظيف كنب الكويت تنظيف كنبات الكويت
ادوات صحيه الكويت ادوات صحيه الكويت
شركة غسيل سجاد الكويت غسيل سجاد الكويت
فني كهربائي منازل الكويت كهربائي منازل في الكويت

شركة تنظيف في الكويت شركة الكويت سيرفيس للتنظيف

Thanks to the author for writing the post, it was quite necessary for me and liked it. I wrote a note on the https://ukbestessays.org/ about this. I will be happy if you read it and accept it. Thank you for your concern.


Khatron Ke Khiladi Season 11 Made In India Reality Show Please Watch This Website Watch Online Khtron Ke khiladi Show Hosted By Rohid Shetty With Faran Khan. Khatron Ke Khiladi Season 11 All Episodes Online Quality Streaming of all Kkk 11 Live.
khatron ke khiladi 11 full episode

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