Fun With LINQ Expressions, Lambda Functions and Ternary Operators

At work, we work with Unity. Unity comes with its own IDE, MonoDevelop.

MonoDevelop sucks. I hate the way it looks. I hate the way it folds code. I hate the way it unfolds everything every time you save. I like the way it finds files and classes but that’s about the only thing I like about it. And I have to use it for debugging.

However, I don’t have to use it for actual coding. I use Visual Studio 2013 because we already have it and it’s a pretty good tool. It does lack some advanced capabilities and so I installed the ReSharper plugin which adds a lot of power to VS and makes it even much more useful.

What I like about ReSharper is that it suggested a lot of improvements to my code and actually taught me more about LINQ expssions and Lamda functions. I already knew and much liked Ternary operators. I think that all of them make code more lean and prettier and easier to read as it makes it more legible in actual English.

The C Trenary operator is “?”. You use it like this: “(condition) ? (variable if true) : (variable if false)”. Much easier than “if (condition) then return variable1 else return variable2”.

Lambda functions are a kind of anonymous function that is much easier and shorter to write. So, instead of writing “delegate(KeyValuePair<long, string> pair) { do something with pair }” and shoving all that into a callback, you just write “pair => do something with pair”. It doesn’t look like a big improvement but it is in the long run.

LINQ expssions are a sort of C# extensions that provide more functionality to a lot of data structures in a way that make them very generic and very easy to understand.

Which brings me to my example of how I used all of them together to get a very powerful operation done in what is, technically, a single line of code that is also easy to read. Code has been obfuscated to protect the innocent.

List<AClass> AClassList = AListOfStringListPairs.Select(stringListPair => stringListPair.Key != specificKey ? stringListPair.Value.First() : null).ToList();

In English, notice the translation isn’t difficult, I take a List of pairs, where each pair is a string and a list of classes, and filter out the heads of every list whose string fulfill my required predicate and put those into a new list which I can later use.

With out LINQ or Lambda, at least, this code would have been 3-4 times longer if not more. So I like this a lot. There is only one problem: Because of an unavoidable constraint of our development method, we can’t use some specific collection method which LINQ uses almost all of the time. So I can’t use most LINQ expssions. Which sucks.

But not as much as MonoDevelop.


Posted in No Category, Practice, Programming, Thinking Out Loud, Work by with 2 comments.

Comments

  • Oded says:

    Lambda expressions are not anonymous functions – anonymous functions (which is what you showed) are lambda expressions. The distinction is subtle (at least as used in most modern programming languages) but it is important.

    The most important thing when you talk about anonymous functions is the understanding that lambda expressions are code that is a value (or officialky – first class value), which means you can pass then around in variables. Worse then that, lambda expressions “remember”what variables where available in the scope where they were defined and can address them even when being executed somewhere else (its what Perl calls “lexical scoping”). Its an encapsulation technique more basic then OOP (and older) – you can think of it like objects in reverse: objects are data that carries it’s own code, while lambda expressions are code that carries its own data :)