RaspberryIO_26/Swan.Tiny/Extensions.Functional.cs

42 lines
1.1 KiB
C#
Raw Normal View History

2019-12-09 17:25:54 +01:00
using System;
using System.Collections.Generic;
namespace Swan {
/// <summary>
/// Functional programming extension methods.
/// </summary>
public static class FunctionalExtensions {
/// <summary>
/// Whens the specified condition.
/// </summary>
/// <typeparam name="T">The type of IEnumerable.</typeparam>
/// <param name="list">The list.</param>
/// <param name="condition">The condition.</param>
/// <param name="fn">The function.</param>
/// <returns>
/// The IEnumerable.
/// </returns>
/// <exception cref="ArgumentNullException">
/// this
/// or
/// condition
/// or
/// fn.
/// </exception>
public static IEnumerable<T> When<T>(this IEnumerable<T> list, Func<Boolean> condition, Func<IEnumerable<T>, IEnumerable<T>> fn) {
if(list == null) {
throw new ArgumentNullException(nameof(list));
}
if(condition == null) {
throw new ArgumentNullException(nameof(condition));
}
if(fn == null) {
throw new ArgumentNullException(nameof(fn));
}
return condition() ? fn(list) : list;
}
}
}