Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created February 24, 2009 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/69880 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/69880 to your computer and use it in GitHub Desktop.
public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool> pred)
{
return items.Aggregate<T, Func<IEnumerable<T>, IEnumerable<T>>>(
x => x, (f, c) => x => f(pred(c) ? (new [] { c }).Concat(x) : x))(Enumerable.Empty<T>());
}
public static IEnumerable<R> Map<T, R>(this IEnumerable<T> items, Func<T, R> proj)
{
return items.Aggregate<T, Func<IEnumerable<R>, IEnumerable<R>>>(
x => x, (f, c) => x => f((new[] { proj(c) }).Concat(x)))(Enumerable.Empty<R>());
}
public static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> items)
{
return items.Aggregate<IEnumerable<T>, Func<IEnumerable<T>, IEnumerable<T>>>(
x => x, (f, c) => x => f(c.Concat(x)))(Enumerable.Empty<T>());
}
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, IEnumerable<T> other)
{
return source.Aggregate<T, Func<IEnumerable<T>,IEnumerable<T>>>(
x => x, (f, c) => x => f((new [] { c }).Concat(x)))(other);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment