<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://mg.to" xmlns:dc="https://purl.org/dc/elements/1.1/">
<channel>
 <title>mg.to - Iterators in C#, Python, and Ruby - Comments</title>
 <link>https://mg.to/2004/08/03/iterator-cpr</link>
 <description>Comments for &quot;Iterators in C#, Python, and Ruby&quot;</description>
 <language>en</language>
<item>
 <title>  Well, yield in ruby surely don&#039;t equal to function</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-11</link>
 <description>&lt;p&gt;Well, yield in ruby surely don&amp;#8217;t equal to function
in javascript or delegate in C#.
  block in ruby is a closure, which means it use the
current context where it&amp;#8217;s in, it means sth like this:&lt;/p&gt;

&lt;p&gt;def fun1
  fun2
end
def fun2
  fun3 {
    return &amp;#8220;hello1&amp;#8221;;
  }&lt;/p&gt;

&lt;p&gt;fun3 {
    return &amp;#8220;hello2&amp;#8221;;
  }&lt;/p&gt;

&lt;p&gt;end
def fun3
  yield
end
result = fun1&lt;/p&gt;

&lt;p&gt;puts result #&amp;#8212;&amp;gt;&amp;#8221;hello1&amp;#8221;&lt;/p&gt;

&lt;h6&gt;###################################3&lt;/h6&gt;

&lt;p&gt;the first block in fun2 will just return &amp;#8220;hello1&amp;#8221;
to the caller fun1, and skip the second block invocation,
but with function pointer or delegate, you sure can&amp;#8217;t do this,
with return keyword in function pointer or delegate, you just
return to where you call the function pointer or delegate.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s the differnce between them.
the block can decide whether it should return some value
to uplevel but function pointer or delegate can&amp;#8217;t&lt;/p&gt;</description>
 <pubDate>Tue, 10 May 2005 19:33:42 +0000</pubDate>
 <dc:creator>femto</dc:creator>
 <guid isPermaLink="false">comment 11 at https://mg.to</guid>
</item>
<item>
 <title>Elton, your trackback had an invalid URL:</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-10</link>
 <description>&lt;p&gt;Elton, your trackback had an invalid URL:&lt;/p&gt;

&lt;p&gt;https://arachnid/Blogs/ewells/archive/2004/08/09/435.aspx&lt;/p&gt;

&lt;p&gt;Want to try again?&lt;/p&gt;</description>
 <pubDate>Tue, 10 Aug 2004 01:34:40 +0000</pubDate>
 <dc:creator>Michael Geary</dc:creator>
 <guid isPermaLink="false">comment 10 at https://mg.to</guid>
</item>
<item>
 <title>Exploring C# 2.0 iterators
</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-9</link>
 <description>&lt;p&gt;&lt;strong&gt;Exploring C# 2.0 iterators&lt;/strong&gt;
&lt;/p&gt;</description>
 <pubDate>Mon, 09 Aug 2004 21:20:01 +0000</pubDate>
 <dc:creator>Elton Wells</dc:creator>
 <guid isPermaLink="false">comment 9 at https://mg.to</guid>
</item>
<item>
 <title>argh. sorry about this. it doesn&#039;t seem to like unescaped &amp;lt;s.</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-8</link>
 <description>&lt;p&gt;argh. sorry about this. it doesn&amp;#8217;t seem to like unescaped &amp;lt;s&amp;#8230;
and again, this time without the huge, and now utterly redundant comment:
&lt;code&gt;
using System;
class C {
    public static void MyIterator(Action yield) {
        Console.WriteLine(&quot;Before First&quot;);
        yield(&quot;First&quot;);
        Console.WriteLine(&quot;After First&quot;);
        for(int i = 0; i &amp;lt; 3; i++)
                    yield(i.ToString());
        Console.WriteLine(&quot;Before Last&quot;);
        yield(&quot;Last&quot;);
        Console.WriteLine(&quot;After Last&quot;);
    }
    public static void Main() {
        MyIterator(delegate(string s) {
            Console.WriteLine(s);
        });
    }
}
&lt;/code&gt;&lt;/p&gt;</description>
 <pubDate>Tue, 03 Aug 2004 12:52:17 +0000</pubDate>
 <dc:creator>mike roome</dc:creator>
 <guid isPermaLink="false">comment 8 at https://mg.to</guid>
</item>
<item>
 <title>damnit. my code got mangled...</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-7</link>
 <description>&lt;p&gt;&lt;/p&gt;</description>
 <pubDate>Tue, 03 Aug 2004 12:48:58 +0000</pubDate>
 <dc:creator>mike roome</dc:creator>
 <guid isPermaLink="false">comment 7 at https://mg.to</guid>
</item>
<item>
 <title>The ruby example isn&#039;t exactly equivalent, from what i understan</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-6</link>
 <description>&lt;p&gt;The ruby example isn&amp;#8217;t exactly equivalent, from what i understand of ruby &amp;#8212; a block defines an anonymous function, which is passed into the method being called, and can be called within the function with &amp;#8216;yield&amp;#8217;. The end result is more or less the same, but the method by which the result is achieved is completely different.&lt;/p&gt;

&lt;p&gt;For what it&amp;#8217;s worth, you could semantically equivalent to the ruby example in C# as well, using anonymous delegates:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-csharp&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;System&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; C &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;/**&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* delegate void Action&amp;lt;t&amp;gt;(T obj); is a standard delegate provided by the&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* 2.0 class library. Others include:&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* bool Predicate&amp;lt;t&amp;gt;(T obj);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* U Converter&amp;lt;t ,U&amp;gt;(T from);&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* int Comparison&amp;lt;t&amp;gt;(T x, T y);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* see https://blogs.msdn.com/kcwalina/archive/2004/06/22/162533.aspx&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* for more details&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;*&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* also, since &#039;yield&#039; isn&#039;t a keyword on its own (it&#039;s only special in a&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* &#039;yield return&#039; or &#039;yield break&#039; statement), i decided to call the&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* delegate &#039;yield&#039;, to make the correspondence to the ruby clearer.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #008080; font-style: italic;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;**/&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #C00000;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #C00000;&quot;&gt;void&lt;/span&gt; MyIterator&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;Action&amp;lt;string&amp;gt; yield&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&quot;Before First&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; yield&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&quot;First&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&quot;After First&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color: #FF0000;&quot;&gt;0&lt;/span&gt;; i &amp;lt; &lt;span style=&quot;color: #FF0000;&quot;&gt;3&lt;/span&gt;; i++&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; yield&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;i.&lt;span style=&quot;color: #0000FF;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&quot;Before Last&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; yield&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&quot;Last&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&quot;After Last&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #C00000;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #C00000;&quot;&gt;void&lt;/span&gt; Main&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; MyIterator&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;delegate&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; s&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;s&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;
using System;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    for(int i = 0; i &amp;lt; 3; i++) yield(i.ToString());

    Console.WriteLine(&quot;Before Last&quot;);
    yield(&quot;Last&quot;);
    Console.WriteLine(&quot;After Last&quot;);
}

public static void Main() {
    MyIterator(delegate(string s) {
        Console.WriteLine(s);
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}
&lt;/p&gt;

&lt;p&gt;(note: i haven&amp;#8217;t tested this, but it should work on Beta1, afaik).&lt;/p&gt;</description>
 <pubDate>Tue, 03 Aug 2004 12:46:57 +0000</pubDate>
 <dc:creator>mike roome</dc:creator>
 <guid isPermaLink="false">comment 6 at https://mg.to</guid>
</item>
<item>
 <title>Let&#039;s iterate over this topic one more time</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-4</link>
 <description>&lt;p&gt;&lt;strong&gt;Let&amp;#8217;s iterate over this topic one more time&lt;/strong&gt;
&lt;/p&gt;</description>
 <pubDate>Tue, 03 Aug 2004 10:33:03 +0000</pubDate>
 <dc:creator>Another Tired Idea</dc:creator>
 <guid isPermaLink="false">comment 4 at https://mg.to</guid>
</item>
<item>
 <title>Michael Geary returns
</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-3</link>
 <description>&lt;p&gt;&lt;strong&gt;Michael Geary returns&lt;/strong&gt;&lt;/p&gt;</description>
 <pubDate>Tue, 03 Aug 2004 09:02:52 +0000</pubDate>
 <dc:creator>Under The Hood - Matt Pietrek</dc:creator>
 <guid isPermaLink="false">comment 3 at https://mg.to</guid>
</item>
<item>
 <title>Iterators in C#, Python, and Ruby</title>
 <link>https://mg.to/2004/08/03/iterator-cpr#comment-2</link>
 <description>&lt;p&gt;&lt;strong&gt;Iterators in C#, Python, and Ruby&lt;/strong&gt;&lt;/p&gt;</description>
 <pubDate>Tue, 03 Aug 2004 02:31:24 +0000</pubDate>
 <dc:creator>Under The Hood - Matt Pietrek</dc:creator>
 <guid isPermaLink="false">comment 2 at https://mg.to</guid>
</item>
<item>
 <title>Iterators in C#, Python, and Ruby</title>
 <link>https://mg.to/2004/08/03/iterator-cpr</link>
 <description>&lt;p&gt;Matt Pietrek marvels at &lt;a href=&quot;https://blogs.msdn.com/matt_pietrek/archive/2004/07/26/197242.aspx&quot;&gt;C#  2.0 iterators&lt;/a&gt; and dissects them right down to the CLR bytecode. I always learn something from Matt, and this whirlwind tour is no exception.&lt;/p&gt;

&lt;p&gt;Matt says, &amp;#8220;This was the beginning of my descent into the loopy world of C# 2.0 iterators. It took me awhile to wrap my head around them, and when I tried to explain them to other team members I got looks of total confusion.&amp;#8221; I wonder if it would have been less confusing if Matt&amp;#8217;s team had first been exposed to &lt;code&gt;yield&lt;/code&gt; iterators in a language that makes them easier to use.&lt;/p&gt;

&lt;p&gt;After using &lt;a href=&quot;https://www.python.org/&quot;&gt;Python&lt;/a&gt; and &lt;a href=&quot;https://www.ruby-lang.org/&quot;&gt;Ruby&lt;/a&gt;, the iterators in C# feel right at home to me. They work the same in all three languages, but in Ruby and Python there&amp;#8217;s not as much other code to get in the way of understanding them.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s combine all of Matt&amp;#8217;s examples into one, and compare the code in each language. First, in C#:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-csharp&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;System&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #C00000;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: #000000;&quot;&gt;System&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Collections&lt;/span&gt;.&lt;span style=&quot;color: #0000FF;&quot;&gt;Generic&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; SomeContainer&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;public&lt;/span&gt; IEnumerable&amp;lt;string&amp;gt; MyIterator&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&quot;Before First&quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; yield &lt;span style=&quot;color: #C00000;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&quot;First&quot;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&quot;After First&quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;int&lt;/span&gt; i = &lt;span style=&quot;color: #FF0000;&quot;&gt;0&lt;/span&gt;;&amp;nbsp; i &amp;lt; &lt;span style=&quot;color: #FF0000;&quot;&gt;3&lt;/span&gt;;&amp;nbsp; i++ &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; yield &lt;span style=&quot;color: #C00000;&quot;&gt;return&lt;/span&gt; i.&lt;span style=&quot;color: #0000FF;&quot;&gt;ToString&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&quot;Before Last&quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; yield &lt;span style=&quot;color: #C00000;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&quot;Last&quot;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&quot;After Last&quot;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;class&lt;/span&gt; foo&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #C00000;&quot;&gt;void&lt;/span&gt;&amp;nbsp; Main&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SomeContainer container =&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;a href=&quot;https://www.google.com/search?q=new+msdn.microsoft.com&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;new&lt;/span&gt;&lt;/a&gt; SomeContainer&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;foreach&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;string&lt;/span&gt; s &lt;span style=&quot;color: #C00000;&quot;&gt;in&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; container.&lt;span style=&quot;color: #0000FF;&quot;&gt;MyIterator&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Console.&lt;span style=&quot;color: #0000FF;&quot;&gt;WriteLine&lt;/span&gt;&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt; s &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;span style=&quot;color: #000000;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When you run that, it should print:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div&gt;Before First&lt;br /&gt;
First&lt;br /&gt;
After First&lt;br /&gt;
0&lt;br /&gt;
1&lt;br /&gt;
2&lt;br /&gt;
Before Last&lt;br /&gt;
Last&lt;br /&gt;
After Last&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here&amp;#8217;s how you would write the same code in Python:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-python&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;def&lt;/span&gt; MyIterator&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;:&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&quot;Before First&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&quot;First&quot;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&quot;After First&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: #C00000;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;xrange&lt;/span&gt;&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff4500;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;:&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&quot;Before Last&quot;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&quot;Last&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;print&lt;/span&gt; &lt;span style=&quot;color: #483d8b;&quot;&gt;&quot;After Last&quot;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;for&lt;/span&gt; s &lt;span style=&quot;color: #C00000;&quot;&gt;in&lt;/span&gt; MyIterator&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: black;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;:&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;print&lt;/span&gt; s&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And in Ruby, the code looks like this:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-ruby&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;def&lt;/span&gt; MyIterator&lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:#CC0066; font-weight:bold;&quot;&gt;p&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;Before First&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;First&quot;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; p &lt;span style=&quot;color:#996600;&quot;&gt;&quot;After First&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:#006666;&quot;&gt;3&lt;/span&gt;.&lt;span style=&quot;color:#9900CC;&quot;&gt;times&lt;/span&gt; &lt;span style=&quot;color: #C00000;&quot;&gt;do&lt;/span&gt; |i|&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; i.&lt;span style=&quot;color:#9900CC;&quot;&gt;to_s&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:#CC0066; font-weight:bold;&quot;&gt;p&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;Before Last&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;Last&quot;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; p &lt;span style=&quot;color:#996600;&quot;&gt;&quot;After Last&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;MyIterator &lt;span style=&quot;color: #C00000;&quot;&gt;do&lt;/span&gt; |s|&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:#CC0066; font-weight:bold;&quot;&gt;p&lt;/span&gt; s&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;end&lt;/span&gt; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The one unfamiliar thing here may be the &lt;code&gt;|name|&lt;/code&gt; notation, which is how a code block such as the body of a loop receives its argument. And the &lt;code&gt;p&lt;/code&gt; statements are a kind of print statement.&lt;/p&gt;

&lt;p&gt;This Ruby version is even more concise and equally readable once you&amp;#8217;re comfortable with the &lt;code&gt;|name|&lt;/code&gt; notation:&lt;/p&gt;

&lt;div style=&quot;padding: 5px !important; border: 1px solid rgb(253,187,134) !important; background-color: rgb(255,253,245) !important; font-family: Verdana,sans-serif !important;&quot;&gt;&lt;div style=&quot;background-color: rgb(255,250,238) !important;&quot;&gt;&lt;div class=&quot;geshi-ruby&quot;&gt;&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;def&lt;/span&gt; MyIterator&lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:#CC0066; font-weight:bold;&quot;&gt;p&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;Before First&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;First&quot;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; p &lt;span style=&quot;color:#996600;&quot;&gt;&quot;After First&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:#006666;&quot;&gt;3&lt;/span&gt;.&lt;span style=&quot;color:#9900CC;&quot;&gt;times&lt;/span&gt; &lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt; |i| &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; i.&lt;span style=&quot;color:#9900CC;&quot;&gt;to_s&lt;/span&gt; &lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:#CC0066; font-weight:bold;&quot;&gt;p&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;Before Last&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #C00000;&quot;&gt;yield&lt;/span&gt; &lt;span style=&quot;color:#996600;&quot;&gt;&quot;Last&quot;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&amp;nbsp; &amp;nbsp; p &lt;span style=&quot;color:#996600;&quot;&gt;&quot;After Last&quot;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;&lt;span style=&quot;color: #C00000;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;background-color:rgb(255,234,216);&quot;&gt;MyIterator &lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#123;&lt;/span&gt; |s| &lt;span style=&quot;color:#CC0066; font-weight:bold;&quot;&gt;p&lt;/span&gt; s &lt;span style=&quot;color:#006600; font-weight:bold;color: #0000FF;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Either way, the Python and Ruby versions make it easier to see what the iterator function does and how &lt;code&gt;yield&lt;/code&gt; interacts with the rest of the code.&lt;/p&gt;

&lt;p&gt;You may note that the Python and Ruby versions don&amp;#8217;t create and instantiate a &lt;code&gt;SomeContainer&lt;/code&gt; class as the C# version does. That&amp;#8217;s true, and it would make the code in those languages a bit longer (but still simpler than the C# code). But, if you don&amp;#8217;t need to&amp;#8212;and you especially don&amp;#8217;t need to when you&amp;#8217;re experimenting and trying to understand a radical new technique like &lt;code&gt;yield&lt;/code&gt; iterators&amp;#8212;why bother?&lt;/p&gt;
</description>
 <comments>https://mg.to/2004/08/03/iterator-cpr#comments</comments>
 <category domain="https://mg.to/topics/programming/c">C#</category>
 <category domain="https://mg.to/topics/programming">Programming</category>
 <category domain="https://mg.to/topics/programming/python">Python</category>
 <category domain="https://mg.to/topics/programming/ruby">Ruby</category>
 <pubDate>Tue, 03 Aug 2004 01:33:25 +0000</pubDate>
 <dc:creator>Michael Geary</dc:creator>
 <guid isPermaLink="false">6 at https://mg.to</guid>
</item>
</channel>
</rss>
