Archive for the ‘yield return’ Category

JavaScript Generators

February 8, 2010 2 comments

These work annoyingly well. Why annoying? Because they’re Mozilla specific. If only we could use them all the time. The other thing that’s annoying is how they are actually better than the C# equivalent.

There are two major advantages compared with C#’s yield return.
Read more…

Categories: C#, javascript, yield return Tags: , , ,

Generic Value Object With Yield Return

June 9, 2009 3 comments

Inspired by this and this, here’s my version.

Jan Van Ryswyck is right to use static reflection, and to do that he "registers" the properties in a list. On the other hand, assuming you’d have a lot of these objects building up in the GC, it might prove important to reduce the number of ancillary allocations going on, so it may be better to avoid creating a list of the properties in every single instance.

Read more…

Lazy Sequence Generation without using Yield Return

June 4, 2009 4 comments

C# has the yield return feature, which makes it easy to write state machines as if they were plain old methods:

Read more…

Making Lambdas and Iterators Play Nicely Together

February 26, 2009 Leave a comment

The major problem with this idea is in my favourite feature of C#. In an iterator (a function containing the yield keyword), the code is transformed into a state machine represented by a class. It has to be chopped up into sections, so the function can be “paused” after each yield return. Those pieces must all be in the same function, so sadly lambdas don’t play well with iterators.

There are a couple of possible future language features which would completely solve this, however.

Read more…

Categories: C#, lambdas, yield return Tags: , ,

Other Examples of Iterators and Async IO

December 9, 2008 2 comments

The idea of returning functions via yield return to simply asynchronous IO programming has a precedent in Microsoft’s Concurrency and Coordination Runtime:

Although they are yielding interfaces, really they are yielding functions:

The ITask interface has only one really important method: Execute, which means that really it’s a function (a delegate in C# terms). I think the functional style makes the whole thing cleaner, but the point is that the idea was already in use in the Microsoft’s CCR, perhaps as long ago as 2005 (although that’s difficult to verify).

More on Jeffrey Richter’s AsyncEnumerator and Functional Programming

December 6, 2008 4 comments

If you do asynchronous programming (or have been put off it in the past by the complexity) and you haven’t already looked at this, then you really should:

I blogged yesterday about an idea for doing the same kind of thing but using lambdas to encapsulate the last remaining bit of complexity. Today I’ve applied the same idea, but with the goal of providing a thin layer that works along with Jeffrey Richter’s AsyncEnumerator class, essentially providing an optional new way of working with it. As I am not especially familiar with the asynchronous APIs, it would be especially stupid for me to try and reinvent the wheel instead of building on the work Jeffrey has already done.

Read more…

Categories: C#, lambdas, yield return Tags: , ,

Asynchronous Sockets with Yield Return of Lambdas

December 5, 2008 2 comments

Update: More in-depth stuff about this

I just watched this video of Jeffrey Richter demonstrating his AsyncEnumerator class:

The key idea is the realisation that yield return takes care of turning a single function into a continuation that can be resumed under the control of some other code, but still provides a natural way to write procedurally. This is just what is needed to manage asynchronous communication. Great stuff.

Read more…

Categories: C#, lambdas, yield return Tags: , ,

Coroutines with IEnumerable and yield return

April 9, 2008 3 comments

There was a brief flurry of excitement about the fancy yield return syntax when the C# 2.0 specification was first published some years ago, and the idea of it being used for creating coroutines.

This quickly died down, as far as I can see anyway. Either people forgot the idea, or it became such boringly standard practice that no one ever thinks to discuss it anymore. I don’t know which, but I think it deserves more attention either way.

There were a few people at the time saying that it was a shame that C# appeared to only support coroutines in a special case where the function returns some weird IEnumerable<T> thing, and wouldn’t it have been better if it was more general?

Read more…

Categories: C#, yield return Tags: ,