Filter and Map in C# - Peter's Software House

Filter and Map in C#

I was reading this post by Sarah Taraporewalla.  And I immediately wanted to see if I could do this with C#.

Here are my results for Filter:

using System;
using System.Collections.Generic;
 
namespace TestFilter
{
    static class Program
    {
        
        static void Main(string[] args)
        {
            // Add a whole bunch of names
            FilteredList list = new FilteredList();
            list.Add("Anne");
            list.Add("Brian");
            list.Add("Pieter");
            list.Add("Wayne");
            list.Add("Shane");
            list.Add("Susan");
            list.Add("Xavier");
 
            // There whould be 7
            Console.WriteLine(list.Count);
 
            // Use a public function to filter the list
            FilteredList filteredList = list.Filter(list.FilterFunction);
            //There should only be 2 stating with 'S'
            Console.WriteLine(filteredList.Count);
            foreach (string name in filteredList)
            {
                Console.WriteLine(name);
            }
 
            // Use one version of a lambda expression
            FilteredList filteredList2 = list.Filter(item => { return item.StartsWith("A"); });
            // Only one starting with 'A'
            Console.WriteLine(filteredList2.Count);
            foreach (string name in filteredList2)
            {
                Console.WriteLine(name);
            }
 
            // A better/shorter version of the previous lambda?  Definately cleaner
            FilteredList filteredList3 = list.Filter(item => item.StartsWith("Pi"));
            // Only one starting with 'Pi'
            Console.WriteLine(filteredList3.Count);
            foreach (string name in filteredList3)
            {
                Console.WriteLine(name);
            }
            
            Console.ReadLine();
        }
 
    }
 
    public class FilteredList : List<string>
    {
        public bool FilterFunction(string item)
        {
            return item.StartsWith("S");
        }
 
        public FilteredList Filter(Func<string, bool> condition)
        {
            FilteredList filteredList = new FilteredList();
            foreach (string item in this)
            {
                if (condition(item))
                {
                    filteredList.Add(item);
                }
            }
            return filteredList;
        }
    }
}

The result:

image

 

Here are my results for Map:

using System;
using System.Collections.Generic;
 
namespace TestMap
{
    static class Program
    {
        static void Main(string[] args)
        {
            VehicleList list = new VehicleList();
            list.Add(new GWiz());
            list.Add(new SmartCar());
            list.Add(new CityCar());
            list.Add(new PeopleMover());
 
            List<int> mappedList = list.Map(list, list.MapFunction);
            foreach (int numberOfSeats in mappedList)
            {
                Console.WriteLine(numberOfSeats);
            }
            Console.ReadLine();
        }
    }
    public class VehicleList : List<Vehicle>
    {
        public int MapFunction(Vehicle item)
        { 
            return item.NumberOfSeats;
        }
        public List<int> Map(VehicleList list, Func<Vehicle, int> mapFunction)
        {
            List<int> mappedList = new List<int>();
            foreach (Vehicle item in list)
            {
                mappedList.Add(mapFunction(item));
            }
            return mappedList;
        }
    }
    public abstract class Vehicle
    {
        public int NumberOfSeats { get; set; }
    }
    public class GWiz : Vehicle
    {
        public GWiz()
        {
            NumberOfSeats = 1;
        }
    }
    public class SmartCar : Vehicle
    {
        public SmartCar()
        {
            NumberOfSeats = 2;
        }
    }
    public class CityCar : Vehicle
    {
        public CityCar()
        {
            NumberOfSeats = 5;
        }
    }
    public class PeopleMover : Vehicle
    {
        public PeopleMover()
        {
            NumberOfSeats = 9;
        }
    }
}

 

The result will be:

image

Reduce is similar to the other two so not going to waste space on that.

So there is a few things I would like to improve.  The most important is to make them more generic.

Published Sunday, August 17, 2008 1:30 PM by Pieter
Filed under:

Comments

# List Filter and Map in C#

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Monday, August 18, 2008 12:36 AM by DotNetKicks.com

# Using Linq to Filter a list

So yesterday I made this post on applying a filter or map on a list.&#160; Now let’s have a look at how

Monday, August 18, 2008 10:41 AM by Peter's Software House

# Lists: Filter, Map and Reduce - and the Magic of IEnumerator.

I have this bad habit. I will frequently stumble upon a blog post describing some new technique, to which

Monday, August 25, 2008 8:16 PM by Honest Illusion

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: