Skip to content

Instantly share code, notes, and snippets.

@dperish
Created February 8, 2015 15:39
Show Gist options
  • Save dperish/f0a4571754f0e4f2cedb to your computer and use it in GitHub Desktop.
Save dperish/f0a4571754f0e4f2cedb to your computer and use it in GitHub Desktop.
Haskell List Extensions for C#
/// <summary>
/// Extends IEnumerable Types to provide the basic
/// Haskell-like list manipulations
/// </summary>
public static class HaskellListExtensions {
/// <summary>
/// Returns the first item of a list
/// </summary>
public static T Head<T>(this IEnumerable<T> xs) {
return xs.First();
}
/// <summary>
/// Returns all list items except the first
/// </summary>
public static IEnumerable<T> Tail<T>(this IEnumerable<T> xs) {
return xs.Skip(1);
}
/// <summary>
/// Returns the last item of a list
/// </summary>
public static T Last<T>(this IEnumerable<T> xs) {
return xs.Last();
}
/// <summary>
/// Returns all list items except for the last
/// </summary>
public static IEnumerable<T> Init<T>(this IEnumerable<T> xs) {
return xs.Take(xs.Count() - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment