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?
These look very similar to extensions in Linq already:
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>orSelect<T>.