Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Map/Reduce List Extensions in C#

I am writing a few extensions to mimic the map and reduce functions in Lisp.

public delegate R ReduceFunction<T,R>(T t, R previous);
public delegate void TransformFunction<T>(T t, params object[] args);

public static R Reduce<T,R>(this List<T> list, ReduceFunction<T,R> r, R initial)
{
     var aggregate = initial;
     foreach(var t in list)
         aggregate = r(t,aggregate);

     return aggregate;
}
public static void Transform<T>(this List<T> list, TransformFunction<T> f, params object [] args)
{
    foreach(var t in list)
         f(t,args);
}

The transform function will cut down on cruft like:

foreach(var t in list)
    if(conditions && moreconditions)
        //do work etc

Does this make sense? Could it be better?

like image 956
Ben McNiel Avatar asked Sep 02 '08 17:09

Ben McNiel


People also ask

What is MAP reduce in functional programming?

Map-reduce is a programming model that has its roots in functional programming. In addition to often producing short, elegant code for problems involving lists or collections, this model has proven very useful for large-scale highly parallel data processing.

Does C# have reduce?

C# Reduce. Reduce is often the one many developers have some difficulty understanding. But it isn't hard at all. Think of it like this: you have a sequence of something, and you also have a function that takes two of these “somethings” and returns one, after doing some processing.

What is C++ map reduce?

A map operation applies a function to each value in a sequence. A reduce operation combines the elements of a sequence into one value. You can use the C++ Standard Library std::transform and std::accumulate functions to perform map and reduce operations.

Does C# have a map function?

There is no built-in Map type in C#. It does, however, include a powerful Dictionary type, which we utilize to map objects. We must define the key type (such as "string") and the value type when using Dictionary.


3 Answers

According to this link Functional Programming in C# 3.0: How Map/Reduce/Filter can Rock your World the following are the equivalent in C# under the System.Linq namespace:

  • map --> Enumerable.Select
  • reduce --> Enumerable.Aggregate
  • filter --> Enumerable.Where
like image 144
Ray Avatar answered Sep 21 '22 05:09

Ray


These look very similar to extensions in Linq already:

//takes a function that matches the Func<T,R> delegate
listInstance.Aggregate( 
    startingValue, 
    (x, y) => /* aggregate two subsequent values */ );

//takes a function that matches the Action<T> delegate
listInstance.ForEach( 
    x => /* do something with x */);

Why is the 2nd example called Transform? Do you intend to change the values in the list somehow? If that's the case you may be better off using ConvertAll<T> or Select<T>.

like image 35
Keith Avatar answered Sep 19 '22 05:09

Keith


I would use the built in Func delegates instead. This same code would work on any IEnumerable. Your code would turn into:

public static R Reduce<T,R>(this IEnumerable<T> list, Func<T,R> r, R initial)
{
     var aggregate = initial;
     foreach(var t in list)
         aggregate = r(t,aggregate);

     return aggregate;
}
public static void Transform<T>(this IEnumerable<T> list, Func<T> f)
{
    foreach(var t in list)
             f(t);
}
like image 2
Jake Pearson Avatar answered Sep 20 '22 05:09

Jake Pearson