Wednesday, January 8, 2014

TEACHING: Study club of MUGI Jadetabek on Developer Track, WEEK 5

Now, it’s week 5! We could not have the workshop within the next week of week 3 because of my busy schedule, sorry!

In this week 5, we decided again to meet at QQ Kopitiam, at Pacific Place at 10:00AM. This scheduled time was agreed upon us by communicating via email before. Unfortunately Wakhid has to take care of his administrative undergraduate submission paperworks so he couldn't attend this week. And Aris also can’t attend this for personal reason. Next time, please be punctual, guys!

Yup, the show must go! Sorry to leave you if you weren’t present, guys! It’s fortunate for Reza and Siti Smile

Now we already have grabbed the basic concept of generics and colections, now we are moving to LINQ concept but with the introduction of advanced Lambda expression.

Why do we have to know Lambda? Because the answer is quite simple but somehow often missed in many LINQ literature: LINQ is composed by anonymous delegate. This anonymous delegate is often written in functional syntax as Lambda, to emphasize that expressiveness of an anonymous function is available to use and to compose nicely.

Let’s revisit the basic sample of SELECT in LINQ to Object:

using System.Diagnostics;
using System.Linq;

namespace SampleAdvancedLambda
{
    class Program
    {
        static void Main(string[] args)
        {
            var processnames = (from p in Process.GetProcesses()
                                select p.ProcessName).ToList();
        }
    }
}

The code above (in bold) is basically the same as:

var processnames = Process.GetProcesses()
    .Select(p => p.ProcessName).ToList();

 

Again, type inference will infer that processnames is a List of String, the type syntax is List<String>. Type inference happens nicely because the select will infer the property of ProcessName, and this property is typed String.

The notation of “=>” in the Select method is a form of denoting Lambda Expression. Although this is a syntactic sugar, it’s quite powerful syntax to replace tedious one line anonymous delegate.

But, what happened inside a select? Basically this is a select method body: (it has to be inside of a static class)

public static class QueryHelper
{
    public static IEnumerable<U> Select<T,U>(this IEnumerable<T> data, Func<T,U> selector)
    {
        foreach (var item in data)
        {
            yield return selector(item);
        }
    }
}

If you look at the signature, there’s “this” keyword to identify that this is an extension method of IEnumerable<T> (the first parameter).

Looks like they were having a little bit difficulties to grab this concept, here are their expression: Smile

WEEK5_01

 

I also gave them some sample cases, like constructing your own ForEach and ForNext (For Each with index) as Lambda.

And Abdullah is joining force to dive this concept!

WEEK5_02

 

Fortunately, they began to see the light that Lambda Expression is not that hard!

WEEK5_03


Further references:

  1. C# Extension method reference in Visual Studio 2010: http://msdn.microsoft.com/en-us/library/bb383977(v=vs.100).aspx
  2. C# Lambda Expression in Visual Studio 2010: http://msdn.microsoft.com/en-us/library/bb397687(v=vs.100).aspx

No comments:

Post a Comment