Repository navigation
Expand file tree
/
Copy pathParityMathExtensions.cs
More file actions
91 lines (81 loc) · 3.46 KB
/
Copy pathParityMathExtensions.cs
File metadata and controls
91 lines (81 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using ByteDecoder.Common.GuardClauses;
namespace ByteDecoder.RoyalLibrary
{
/// <summary>
///
/// </summary>
public static class ParityMathExtensions
{
/// <summary>
/// Even expression evaluation delegate type
/// </summary>
public static readonly Func<int, bool> EvenPredicate = value => value % 2 == 0;
/// <summary>
/// Odd expression evaluation delegate type
/// </summary>
public static readonly Func<int, bool> OddPredicate = value => value % 2 != 0;
/// <summary>
/// Return an array of evens integers from an integer source collection
/// </summary>
/// <param name="numbers">Integer source collection</param>
/// <returns></returns>
public static IEnumerable<int> Evens(this IEnumerable<int> numbers) => numbers.Where(EvenPredicate);
/// <summary>
/// Deferred execution
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="selector"></param>
/// <returns></returns>
public static IEnumerable<T> Evens<T>(this IEnumerable<T> source, Func<T, int> selector) =>
source.ParityEvaluator(EvenPredicate, selector);
/// <summary>
/// Return an array of odds integers from an integer source collection
/// </summary>
/// <param name="numbers">Integer source collection</param>
/// <returns></returns>
public static IEnumerable<int> Odds(this IEnumerable<int> numbers) => numbers.Where(OddPredicate);
/// <summary>
/// Deferred Execution
/// </summary>
/// <param name="source"></param>
/// <param name="selector"></param>
/// <returns></returns>
public static IEnumerable<T> Odds<T>(this IEnumerable<T> source, Func<T, int> selector) =>
source.ParityEvaluator(OddPredicate, selector);
/// <summary>
/// Return a sum of all evens integers from an integer source collection
/// </summary>
/// <param name="numbers">Integer source collection</param>
/// <returns></returns>
public static long TotalAllEvens(this IEnumerable<int> numbers) => numbers.Evens().LongSum();
/// <summary>
/// Return a sum of all odds integers from an integer source collection
/// </summary>
/// <param name="numbers">Integer source collection</param>
/// <returns></returns>
public static long TotalAllOdds(this IEnumerable<int> numbers) => numbers.Odds().LongSum();
/// <summary>
/// Deferred execution
/// </summary>
/// <param name="source"></param>
/// <param name="evaluatorPredicate"></param>
/// <param name="selector"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IEnumerable<T> ParityEvaluator<T>(this IEnumerable<T> source, Func<int, bool> evaluatorPredicate, Func<T, int> selector)
{
Guard.Break.IfArgumentIsNull(source, nameof(source));
Guard.Break.IfArgumentIsNull(evaluatorPredicate, nameof(evaluatorPredicate));
Guard.Break.IfArgumentIsNull(selector, nameof(selector));
foreach (var item in source)
{
if (evaluatorPredicate(selector(item)))
yield return item;
}
}
}
}