forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumerableEx.cs
More file actions
50 lines (44 loc) · 1.72 KB
/
EnumerableEx.cs
File metadata and controls
50 lines (44 loc) · 1.72 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
using System;
using System.Collections.Generic;
namespace Simple.Data
{
using System.Collections.ObjectModel;
public static class EnumerableEx
{
public static Func<Maybe<T>> ToIterator<T>(this IEnumerable<T> source)
{
var enumerator = source.GetEnumerator();
return
() => enumerator.MoveNext() ? enumerator.Current : Maybe<T>.None;
}
public static IReadOnlyDictionary<TKey, TValue> ToReadOnlyDictionary<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
return ToReadOnlyDictionary(source, EqualityComparer<TKey>.Default);
}
public static IReadOnlyDictionary<TKey, TValue> ToReadOnlyDictionary<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
{
ICollection<KeyValuePair<TKey, TValue>> dict = new Dictionary<TKey, TValue>(comparer);
foreach (var pair in source)
{
dict.Add(pair);
}
return new ReadOnlyDictionary<TKey, TValue>((IDictionary<TKey, TValue>) dict);
}
public static IEnumerable<T> Once<T>(T item)
{
yield return item;
}
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> sourcePairs)
{
ICollection<KeyValuePair<TKey, TValue>> dict = new Dictionary<TKey, TValue>();
foreach (var pair in sourcePairs)
{
dict.Add(pair);
}
return (IDictionary<TKey, TValue>) dict;
}
}
}