<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx</link><description>Ed "Scripting Guy" Wilson was kind enough to ask me to be a guest commentator at this years Summer Scripting Games , which have just completed. I've been working on a series for this blog about some unusual cases in the design of the "iterator block"</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9805965</link><pubDate>Sat, 27 Jun 2009 00:00:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805965</guid><dc:creator>Strilanc</dc:creator><description>&lt;p&gt;I generally prefer pulling to pushing. The reason I prefer it is because when you are being pushed data, it's sometimes hard to know if you have enough data yet without doing half the work then aborting.&lt;/p&gt;
&lt;p&gt;For example, I bet the compression stream Write methods are more complicated than the Read methods, because they have to wait for enough data before they can emit the next byte. The read method just asks for the data it wants, and that's it.&lt;/p&gt;
&lt;p&gt;I don't like it when things are needlessly complicated, so I decided to try to turn allow pull methods to accept 'push' data. The major challenge is the lack of continuations or coroutines in .Net, so I used a separate thread instead. When data arrives the push thread passes control to the processing thread, which runs until it requests more data than is available, at which point it passes control back to the push thread. Unfortunately the requirement of that extra thread makes this only useful in limited cases.&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9805985</link><pubDate>Sat, 27 Jun 2009 00:18:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805985</guid><dc:creator>Dave</dc:creator><description>&lt;p&gt;I thought a commentator was supposed to make mindless comments! Where were the corny sports metaphors?&lt;/p&gt;
&lt;p&gt;&amp;quot;To win this competition, that script will really need to go out and execute. Really, you just need a little bit of I/O to keep the drive alive. &amp;nbsp;That may not be a full line of output...it depends on where they spot the cursor. A new Dictionary object would give him some more room to operate. That runtime error was definitely due to a blown coverage test. Now that was an ill-advised pass through the loop. He'll try and tack on the extra point with an autoincrement.&amp;quot;&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9806051</link><pubDate>Sat, 27 Jun 2009 01:29:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806051</guid><dc:creator>marc.gravell</dc:creator><description>&lt;p&gt;&amp;quot;Similarly, collection iterators could be implemented on a “push” model.&amp;quot; - you hay have already seen it, but if not, take a peek at PushLINQ in MiscUtil; Jon and I had fun making this work as a full LINQ implementation where data is pushed, not pulled.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://msmvps.com/blogs/jon_skeet/archive/2008/01/04/quot-push-quot-linq-revisited-next-attempt-at-an-explanation.aspx"&gt;http://msmvps.com/blogs/jon_skeet/archive/2008/01/04/quot-push-quot-linq-revisited-next-attempt-at-an-explanation.aspx&lt;/a&gt; &amp;amp; &lt;a rel="nofollow" target="_new" href="http://www.yoda.arachsys.com/csharp/miscutil/"&gt;http://www.yoda.arachsys.com/csharp/miscutil/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9806063</link><pubDate>Sat, 27 Jun 2009 01:52:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806063</guid><dc:creator>Bertrand Le Roy</dc:creator><description>&lt;p&gt;If I'm not mistaken, one example of collection iterators that push is SAX. &lt;a rel="nofollow" target="_new" href="http://en.wikipedia.org/wiki/Simple_API_for_XML"&gt;http://en.wikipedia.org/wiki/Simple_API_for_XML&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9806091</link><pubDate>Sat, 27 Jun 2009 02:33:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806091</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;&amp;gt; The next version of the CLR framework is likely to have standard interfaces that represent “observable collections”&lt;/p&gt;
&lt;p&gt;Do you mean 4.0, or the next version after that?&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9806092</link><pubDate>Sat, 27 Jun 2009 02:35:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806092</guid><dc:creator>Pavel Minaev [MSFT]</dc:creator><description>&lt;p&gt;Oh, and I also can't help but remember F#'s IEvent, which can work in either pull or push. Given the ongoing type unification between languages (e.g. F# and IronPython separate tuples being replaced by a single common System.Tuple), I wonder if that upcoming feature is also going to be used in a similar way - for IEvent by F#, and possibly for something similar in C#?&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9806255</link><pubDate>Sat, 27 Jun 2009 06:33:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806255</guid><dc:creator>Ben Voigt [C++ MVP]</dc:creator><description>&lt;p&gt;@Strilanc:&lt;/p&gt;
&lt;p&gt;Keeping state and using 'push' is the price you pay for non-blocking operation. &amp;nbsp;But who said C# doesn't have coroutines? &amp;nbsp;Since Eric is talking about iterators, it's the right place to point out that iterator blocks (the &amp;quot;yield return&amp;quot; construct) are nothing other than coroutines.&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9806456</link><pubDate>Sat, 27 Jun 2009 11:27:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806456</guid><dc:creator>Frank Quednau</dc:creator><description>&lt;p&gt;&amp;gt; The next version of the CLR framework is likely to have standard interfaces that represent “observable collections”&lt;/p&gt;
&lt;p&gt;There is already INotifyCollectionChanged and INotifyPropertyChanged. Or do you mean something like the abstraction of parsing &amp;amp; registering callbacks and filters to have more control over when your code is called?&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9806518</link><pubDate>Sat, 27 Jun 2009 12:52:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806518</guid><dc:creator>marc.gravell</dc:creator><description>&lt;p&gt;&amp;gt; There is already INotifyCollectionChanged and INotifyPropertyChanged.&lt;/p&gt;
&lt;p&gt;Don't forget IBindingList.ListChanged, which (according to MSDN) goes all the way back to .NET 1.1&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9807659</link><pubDate>Mon, 29 Jun 2009 00:46:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9807659</guid><dc:creator>ShuggyCoUk</dc:creator><description>&lt;p&gt;&amp;quot;Why exactly is this exciting? &amp;nbsp;Or are we really just that impressed at someone reinventing pthread_cond_wait?&amp;quot;&lt;/p&gt;
&lt;p&gt;imagine a fold on an infinite sequence where the accumulator is always available to query at any point.&lt;/p&gt;
&lt;p&gt;All your usual tools/functions for dealing with sequences would 'just work', I like the expressibiity implied.&lt;/p&gt;
&lt;p&gt; The compiler can transform something like this already&lt;/p&gt;
&lt;p&gt;public static IEnumerable&amp;lt;TResult&amp;gt; FoldWithPeek(this IEnumerable&amp;lt;T&amp;gt; s, TResult acc, Func&amp;lt;TResult,T,TResult&amp;gt; op)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;yield return acc;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;foreach (var t in s)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;acc = op(acc,t);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;yield return acc;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;or you could be more imperative in style&lt;/p&gt;
&lt;p&gt;public static TResult FoldWithPeek(this IEnumerable&amp;lt;T&amp;gt; s, ref TResult acc, Func&amp;lt;TResult,T,TResult&amp;gt; op)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;foreach (var t in s)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;acc = op(acc,t);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;return acc;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;and the compiler inserts the magic to let acc be read (but not written to) elsewhere if you need to refer to it. I prefer the co-routine style (you are letting the compiler/platform deal with the underlying thread management (the ref value wouldn't play nice if the stack moved for example), heck it could use fibers or somesuch if it wanted to.&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9808374</link><pubDate>Mon, 29 Jun 2009 12:21:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9808374</guid><dc:creator>Anthony Tarlano</dc:creator><description>&lt;p&gt;If you haven't seen this talk by Erik Meijer at Lang.NET 2009 you should give it a viewing.. &lt;/p&gt;
&lt;p&gt;He pretty much nails &amp;quot;the powerful isomorphism between events and collections&amp;quot; by using combinators with IObserverable reactive LINQ queries instead of IEnumerables..&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.langnetsymposium.com/2009/talks/23-ErikMeijer-LiveLabsReactiveFramework.html"&gt;http://www.langnetsymposium.com/2009/talks/23-ErikMeijer-LiveLabsReactiveFramework.html&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9811305</link><pubDate>Wed, 01 Jul 2009 18:52:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9811305</guid><dc:creator>Greg</dc:creator><description>&lt;p&gt;The easier way to do this is to use powershell to build a batch file for your to change the process's priority. &amp;nbsp;You would then difference (using FC.exe) this batch file with the last batch file you had to find new lines (i.e., new processes) and output that to a batch file. &amp;nbsp;Run that batch file. &amp;nbsp;Rename your output file to old.txt and use it for the next run's file compare.&lt;/p&gt;
&lt;p&gt;I've used this trick of building a batch file via command line tools many times to simplify what would involve lots of vbscript or win32 api coding.&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9822297</link><pubDate>Tue, 07 Jul 2009 14:28:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9822297</guid><dc:creator>Rainer Schuster</dc:creator><description>&lt;p&gt;Great news! Sound like the ReactiveLinq &lt;a rel="nofollow" target="_new" href="http://tomasp.net/blog/reactive-ii-csevents.aspx"&gt;http://tomasp.net/blog/reactive-ii-csevents.aspx&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: Iterators at the Summer Games</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/26/iterators-at-the-summer-games.aspx#9878640</link><pubDate>Fri, 21 Aug 2009 16:32:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9878640</guid><dc:creator>quetzalcoatl</dc:creator><description>&lt;p&gt;Though a nice article, take no offence: sounds like you've just found a wheel.&lt;/p&gt;
&lt;p&gt;This isomorphism is well-known (at least from my point of view) and is also used in VERY many places.&lt;/p&gt;
&lt;p&gt;Take for example plain old send/receive pair for berkeley sockets. RECV is blocking data reading mechanisms. Almost wherever you look at OOP send/recv-like communication, you find that recv is loop'ed to wait until whole object is downloaded and then returns the whole object. More bright C++'ers envelop that method in an .. iterator, to use such sequence easily with whole STL/Boost/etc libraries. Mind the whole &amp;quot;stream/collection&amp;quot; approach directly leads to this. Yep, writing your own iterator is not a task for a complete beginner, but after writing two or three templates, its asy enough to consider it a tool rather than esoteric feature.&lt;/p&gt;
&lt;p&gt;Or look at Ruby or Lua, where lambdas thrive as the blocks/procs/coroutines, implementing such dynamic sequence iterators is so plain dumb trivial, that there's hard NOT TO write any streaming iterators at all. if you want to read lines from file and parse them, you open the file, call &amp;quot;enumeratormethod&amp;quot; and supply it with your callback procedure, that will be called back on every single line found in the file. Similar for sockets, webrequests, whatever. It's literally everywhere.&lt;/p&gt;
&lt;p&gt;Totalling up: whenever you perform blocking read from whatever source, you actually use this isomorphism, and it is well-known that every blocking read can be transformed into- and backfrom- an 'on completion' callback. Existence of AsyncIO proves that well enough.&lt;/p&gt;
</description></item></channel></rss>