Editorials

Lambdas Rock

Readers Comments

Daniel: RE: Corporate DNA

I like this article. I think I would add…

– Stewardship of your organizations skillset inventory and toolset.

I think to keep an organisations dna intact and progressive, you need to feed not only your skillset but those of your peers and interested parties.

Many people intend to do well for the organization but lack the knowledge of how to go about meshing and ultimately contributing to the corporate dna.

This is where collaboration meets execution… Modeling and mentoring, whether formal or informal can help teach a eay to contribute and partake in the corporate dna experience.

Lambda’s Rock

I can’t believe it took Java so long to release an implementation of Lambda expressions. But they have, and life is now balanced as it should be.

Why would I speak about Lambda expressions as if they were important? Because, for a programmer, having Lambda Expressions available can save you a considerable amount of effort, while still writing powerful code. Since SSWUG has a strong following of SQL developers, you probably recognize Lambdas through the use of Linq expressions. Without the support of Lambdas, Linq would not be possible.

Another way you experience the foundation of Lambda expressions is the ability to declare a variable using the keyword “var”. var is not a variant data type. It is a data type that is determined at compile time; the type cannot change; it must follow all the constraints of an explicitly typed variable.

I enjoy using patterns that make software flexible and re-usable. The strategy pattern is one of the first patterns many developers learn, because it allows for a lot of flexibility and different behavior to be aligned with a common client request.

A strategy consists of a common interface, and a number of implementations of that interface. The client simply requests one of the implementations be assigned to a local variable defined as the interface. Since the implementation inherits from the interface it replaces the interface variable. Now the client can perform any method, or access any property defined by the interface, without knowing anything about the implementation.

A strategy implementation can take a number of classes to fully develop. This can result in class sprawl, making your project cumbersome and harder to follow. Sometimes you can implement a similar capability using Lambda expressions. If the differences in implementation rely only on a single method, you can use a delegate in place of an interface, and use different lambda expressions to implement the different behaviors desired, by fulfilling the contract of the delegate in each expressions.

Here is a really simple example that, although not be very valuable, demonstrates the method.

public delegate decimal MathOperation(decimal value1, decimal value2);

public class Example

{

public MathOperation add;

public MathOperation subtract;

public MathOperation multiply;

public MathOperation divide;

public Example()

{

add = (val1, val2) => val1 + val2;

subtract = (val1, val2) => val1 - val2;

multiply = (val1, val2) => val1 * val2;

divide = (val1, val2) => val1 / val2;

}

}

public class UseExample

{

public int Main()

{

// formula (((2 * 10) - 5) / 5) + 1

var myMath = new Example();

var result = myMath.multiply(2, 10); // 100

result = myMath.subtract(result, 5); // 95

result = myMath.divide(result, 5); // 19

result = myMath.add(result, 1); // 20

Console.WriteLine("Formula Result = {0}", result);

}

}

While this isn’t a practical implementation, you can see how it would be useful to be able to have one or more variables meeting the prototype of the defined delegate, each having very different behavior. If all you need is a difference in a method implementation, then this can be a very useful technique to learn.

How do you use Lambda expressions? Leave a comment or drop a note to btaylor@sswug.org.

Cheers,

Ben