using System; using System.Collections.Generic; namespace JM.LinqFaster { public static partial class LinqFaster { // -------------------------- Arrays -------------------------------------------- /// /// Returns the first element of an array. /// /// The array to return the first element of. /// The first element in the specified array. public static T FirstF(this T[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { throw Error.NoElements(); } return source[0]; } /// /// Returns the first element in an array that satisfies a specified condition. /// /// An array to return an element from. /// A function to teast each element for a condition. /// The first element that satisfies the condition. public static T FirstF(this T[] source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } for (int i = 0; i < source.Length; i++) { if (predicate(source[i])) { return source[i]; } } throw Error.NoMatch(); } /// /// Returns the first element of an array, or a default value if the /// array contains no elements. /// /// The array to return the first element of. /// default value if source is empty, otherwise, the first element /// in source. public static T FirstOrDefaultF(this T[] source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Length == 0) { return default(T); } return source[0]; } /// /// Returns the first element of the sequence that satisfies a condition or a /// default value if no such element is found. /// /// An IEnumerable to return an element from. /// A function to test each element for a condition. /// public static T FirstOrDefaultF(this T[] source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } for (int i = 0; i < source.Length; i++) { if (predicate(source[i])) { return source[i]; } } return default(T); } // -------------------------- Lists -------------------------------------------- /// /// Returns the first element of a list /// /// The list to return the first element of. /// The first element in the specified list. public static T FirstF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { throw Error.NoElements(); } return source[0]; } /// /// Returns the first element in a list that satisfies a specified condition. /// /// An list to return an element from. /// A function to teast each element for a condition. /// The first element in the list that satisfies the condition. public static T FirstF(this List source, Predicate predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } var firstIndex = source.FindIndex(predicate); if (firstIndex == -1) throw Error.NoMatch(); else return source[firstIndex]; } /// /// Returns the first element of an array, or a default value if the /// array contains no elements. /// /// The array to return the first element of. /// default value if source is empty, otherwise, the first element /// in source. public static T FirstOrDefaultF(this List source) { if (source == null) { throw Error.ArgumentNull("source"); } if (source.Count == 0) { return default(T); } return source[0]; } /// /// Returns the first element of the sequence that satisfies a condition or a /// default value if no such element is found. /// /// An IEnumerable to return an element from. /// A function to test each element for a condition. /// public static T FirstOrDefaultF(this List source, Predicate predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } var firstIndex = source.FindIndex(predicate); if (firstIndex == -1) return default(T); else return source[firstIndex]; } } }