-
Notifications
You must be signed in to change notification settings - Fork 15
IEnumerableExtensions
LINQ provides a set of methods that either return the value of an element of a collection, or return the type's default value if not. These methods suffer from two problems:
- If the type is a reference type, then the default value is null. This forces the developer into handling cases where the method returns null, preventing method chaining for example.
- If, for example,
FirstOrDefault
is called on a list ofint
values and0
is returned, the developer has no way of knowing whether that's because the first element of the array contained0
, or because the list was empty.
Both cases are potential problems that the developer must guard against with defensive code: checking for null and testing the collection's size as well as requesting the element required.
Succinc<T> provides alternatives to these methods by returning an Option<T>
. If the element was found, then a value is returned; otherwise None
is returned. This addresses both of the above potential problems.
(For more details on interacting with the returned option, see the Option<T>
page)
The extension methods supplied by Succinc<T> are:
TryElementAt
TryFirst
TryLast
TrySingle
public Option<T> IEnumerable<T>.TryElementAt(int index)
If the element at location index
exists, Some<T>
will be returned, containing the value at that location. If the collection is smaller than that required to access the element at location index
, then None
is returned.
Two versions of TryFirst
are supported:
public Option<T> IEnumerable<T>.TryFirst()
If the collection contains one or more elements, the value at index 0 is returned via Some<T>
. Otherwise, None
is returned.
public Option<T> IEnumerable<T>.TryFirst(Func<T, bool> predicate)
For each element of the collection, predicate
is invoked with the value of that element. If the predicate returns true
, the enumeration of the collection stops and that value is returned via Some<T>
. If the end of the collection is reached with no match, then None
is returned.
Two versions of TryLast
are supported:
public Option<T> IEnumerable<T>.TryLast()
If the collection contains one or more elements, the value at the last index of the collection is returned via Some<T>
. Otherwise, None
is returned.
public Option<T> IEnumerable<T>.TryLast(Func<T, bool> predicate)
For every element of the collection, predicate
is invoked with the value of that element. If the predicate returns true
, that value is remembered (overriding any previous match) and the enumeration of the collection continues. If one or more matches occurred, the last remembered value is returned via Some<T>
. If the end of the collection is reached with no match, then None
is returned.
public Option<T> IEnumerable<T>.TrySingle(Func<T, bool> predicate)
For each element of the collection, predicate
is invoked with the value of that element. When the predicate first returns true
, the value is remembered and the enumeration of the collection continues. If another predicate invocation then returns true
, more than one match has occurred, so None
is returned (there is no single element that matches the condition). If the end of the collection is reached with just one match, that value is returned via Some<T>
. If no match occurred, then None
is returned.
Action
/Func
conversionsCycle
methods- Converting between
Action
andFunc
- Extension methods for existing types that use
Option<T>
- Indexed enumerations
IEnumerable<T>
cons- Option-based parsers
- Partial function applications
- Pattern matching
- Pipe Operators
- Typed lambdas
Any
Either<TLeft,TRight>
None
Option<T>
Success<T>
Union<T1,T2>
Union<T1,T2,T3>
Union<T1,T2,T3,T4>
Unit
ValueOrError