SIGN UP MEMBER LOGIN:    
ARTICLE

Secrets of Map and Filter Functions

Posted by Sateesh Arveti Articles | Visual C# February 19, 2009
This article explains about C# Implementation of Map and Filter Functions
Reader Level:
Download Files:
 

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:



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:



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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor