Schwartzian Transform in C# 3.0, Part II


Scott linked to my previous post on the Schwartzian Transform. Not long after, Randal L. Schwartz himself (!!) drops by with a comment:

Wow. Interesting to see how different it looks in C#. Of course, I think the original in Perl still looks the most elegant. )

As if response, Elton Wells comes up with an implementation in LINQ. I have to admit, I wasn’t familiar with LINQ at the time that I wrote my previous post–Elton is right, LINQ is just the right tool for this. But I think we can get it a little tighter still:

    return from item in items
           orderby converter(item)
           select item;

If you want the results as a list instead of an IEnumerable:

    return (from item in items
            orderby converter(item)
            select item).ToList();

I can’t make up my mind whether this is cheating or not–it’s really LINQ that’s implemented the Schwartzian transform now. Still, it’s probably the way one would actually do it in C# 3.0.

And best of all, throw in PLINQ and all of a sudden we’re talking about Parallel Schwartzian Transform. Sweet!

3 Responses to “Schwartzian Transform in C# 3.0, Part II”

  1. “I see your Parallel Schwartz is as big as mine!”

  2. Very nice. So much for the literal translations :-). It’s cool to see the most straight forward approach ended up being the best.

  3. 3 farhad

    wow this is brilliant.. thanks dude..

%d bloggers like this: