Archive

Archive for the ‘lambdas’ Category

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: , ,

Displaying a nested evaluation tree from Expression<Func>

February 13, 2009 2 comments

Updated: The downloadable source (see link below) is now tidied up a bit, and also displays any embedded string comparisons in a similar way to NUnit.

This might be useful as a way to write Assert statements in tests. Instead of requiring many different forms of Assert to capture values and intents, we could just have one Assert that accepts a lambda:

Read more…

A functional replacement for the using statement

February 8, 2009 Leave a comment

The using-statement is just that: a statement. Why does this bug me?

The FileStream object has a Length property. Assuming I have a function Open that returns a FileStream ready for us, it is awfully tempting to do this:

Read more…

Categories: C#, IDisposable, lambdas Tags: , ,

Linq to C++0x

January 26, 2009 4 comments

Updated 2011-10-31Major update based on boost range adaptors!!

C++ has long had some functional-styled things in its standard library, in particular a header file called <functional>. But actually using it in a functional way has been painful, largely due to the lack of lambdas.

But C++0x (very good Wikipedia page, by the way) solves this, amongst many other long-standing problems.

With lambdas in place, the next thing I naturally wonder is: what about a complete system of lazy-evaluated list processing, which is to say: what about Linq to Objects in C++?

Read more…

Categories: C#, lambdas, LINQ Tags: , ,

Optimizing Aggregate for String Concatenation

December 10, 2008 5 comments

Linq lets you think in a very generally applicable way and solve a very wide variety of problems with a few key concepts. That’s a great thing. But it’s irritating when the elegant solution doesn’t perform as well as an ugly special case.

Using a combination of well-known Linq features I’m going to demonstrate that we already have the power to get the best of both worlds: speed and elegance.

One example that has always irked me (and which is simple enough to demonstrate the idea with) is this:

Enumerable.Range(0, size) 

Read more…

Categories: C#, expressions, lambdas, LINQ Tags: , , ,

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:

http://msdn.microsoft.com/en-us/magazine/cc546608.aspx

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: , ,