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!
Filed under: C#, LINQ | 3 Comments
“I see your Parallel Schwartz is as big as mine!”
Very nice. So much for the literal translations :-). It’s cool to see the most straight forward approach ended up being the best.
wow this is brilliant.. thanks dude..