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 7Console.WriteLine(list.Count);
// Use a public function to filter the listFilteredList 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:
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:
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.
Comments