예제 #1
0
 /// <summary>
 /// Applies the given function to each element of the sequence and returns the list comprised of the results
 /// for each element where the function returns non-empty value.
 /// </summary>
 /// <param name="source">The input sequence.</param>
 /// <param name="chooser">A function to transform items.</param>
 /// <returns>The result sequence.</returns>
 /// <exception cref="T:System.ArgumentNullException">
 /// Thrown if <paramref name="source" /> or <paramref name="chooser" /> is <c>null</c>.
 /// </exception>
 public static IEnumerable <TResult> MaybeChoose <TSource, TResult>(this IEnumerable <TSource> source, Func <TSource, Maybe <TResult> > chooser)
 {
     RuntimeAssert.ArgumentNotNull(source, nameof(source));
     RuntimeAssert.ArgumentNotNull(chooser, nameof(chooser));
     foreach (var item in source)
     {
         if (chooser(item).TryGetValue(out var result))
         {
             yield return(result);
         }
     }
 }
예제 #2
0
 /// <summary>
 /// Creates new object comparer. Both order and eqaulity comparison are performed using
 /// <paramref name="comparer" /> and hash code generated using <paramref name="getHashCode" />.
 /// </summary>
 /// <param name="comparer">Source comparer.</param>
 /// <param name="getHashCode">Source get hash code function.</param>
 /// <returns>Newly created object comparer.</returns>
 public static ObjectComparer <T> Create(IComparer <T> comparer, Func <T, int> getHashCode)
 {
     RuntimeAssert.ArgumentNotNull(comparer, nameof(comparer));
     RuntimeAssert.ArgumentNotNull(getHashCode, nameof(getHashCode));
     return(new ExplicitObjectComparer(comparer, getHashCode));
 }
예제 #3
0
 /// <summary>
 /// Returns the first element of a sequence that setisfies specified predicate if any or empty container.
 /// </summary>
 /// <param name="source">Source sequence.</param>
 /// <param name="predicate">Predicate to apply.</param>
 /// <returns>
 /// Either empty <see cref="T:NCoreUtils.Maybe{TResult}" /> or
 /// <see cref="T:NCoreUtils.Maybe{TResult}" /> containing the first element.
 /// </returns>
 public static Maybe <T> MaybeFirst <T>(this IEnumerable <T> source, Func <T, bool> predicate)
 {
     RuntimeAssert.ArgumentNotNull(source, nameof(source));
     return(source.Where(predicate).Select(Maybe.AsMaybe).FirstOrDefault());
 }