Secrets of Map and Filter Functions

In this article, we will look into C# implementation of Map and Filter functions. In general Map function transforms a list by applying a function/expression to each of its elements. Finally, it returns value in the form of a list having elements with function applied on its elements. Map function takes two inputs, one is the list and other is function to apply on the elements. Filter function takes input as a list, a predicate function and returns a list of elements from the input collection for which the predicate is true.

First, we will discuss about Map functions followed by Filter functions. Create a new Console application in VS 2008 and name it as MapandFilterFuncSample.

Now, add the below code to Main method:

        static void Main(string[] args)
        {
            //Map Functions.
            int[] myList = { 10, 20, 30, 40, 60 };
            var myDblList = myList.Select(item => item * 2);
            var myDblList1 = from item in myList select item * 2;
            Console.WriteLine("Using Lambda Expressions");
            foreach (int item in myDblList)
            {
                Console.WriteLine(item.ToString());
            }
            Console.WriteLine("Using LINQ");
            foreach (int item in myDblList1)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadLine();
        }

Here, we are applying expression item * 2 for all the elements in myList using Select extension method and Linq. This function iterate through each element in the list and applies the supplied function on those and returns a new list back. Run the application, the output will be like this:

sec1.gif

Now, we will move over Filter functions. Add the below code to Main method:

   static void Main(string[] args)
        {
            //Filter Functions.
            int[] myNumbers = { 10, 20, 30, 11, 21, 31 };
            var myEvenNbrs = myNumbers.Where(nbr => nbr % 2 == 0);
 var myEvenNbrs1 = from nbr in myNumbers where nbr % 2 == 0 select nbr;
            Console.WriteLine("Using Lambda Expressions");
            foreach (int item in myEvenNbrs)
            {
                Console.WriteLine(item.ToString());
            }
            Console.WriteLine("Using LINQ");
            foreach (int item in myEvenNbrs1)
            {
                Console.WriteLine(item.ToString());
            }
            Console.ReadLine();
        }

Here, we are applying expression nbr % 2 == 0 for all the elements in myNumbers using Where extension method and Linq. This function iterate through each element in the list and applies the predicate on those and return elements, that are satisfying the predicate. Run the application, the output will be like this:

sec2.gif

Accumulator/fold functions including above functions uses internal Iterators for its working. Internal iterators takes a collection, a function/expression and applies that on every element of the collection.

Internal Iterators are having following benefits:

  1. Easy to use
  2. Less code [No need of writing external loops],
  3. Reduced errors and
  4. Hides implementation details of the collection.

I am ending the things here. I am attaching source code for reference. I hope this article will be helpful for all.

Up Next
    Ebook Download
    View all
    Learn
    View all