Created
February 8, 2015 15:39
-
-
Save dperish/f0a4571754f0e4f2cedb to your computer and use it in GitHub Desktop.
Haskell List Extensions for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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