forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionHandlers.cs
More file actions
38 lines (34 loc) · 1.2 KB
/
FunctionHandlers.cs
File metadata and controls
38 lines (34 loc) · 1.2 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Simple.Data.QueryPolyfills
{
class FunctionHandlers
{
private static readonly Dictionary<string, Func<IEnumerable<object>, object>> Funcs
= new Dictionary<string, Func<IEnumerable<object>, object>>(StringComparer.OrdinalIgnoreCase)
{
{ "min", o => o.Min() },
{ "max", o => o.Max() },
{ "sum", o => o.Aggregate(ObjectMaths.Add) },
{ "avg", Average},
{ "average", Average},
};
public static bool Exists(string function)
{
return Funcs.ContainsKey(function);
}
public static Func<IEnumerable<object>, object> Get(string function)
{
return Funcs[function];
}
static object Average(IEnumerable<object> source)
{
var list = source.ToList();
if (list.Count == 0) return 0;
var total = list.Aggregate(ObjectMaths.Add);
return ObjectMaths.Divide(total, list.Count);
}
}
}