Here are the examples of the csharp api System.Func.Invoke(TSource) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
717 Examples
19
View Source File : EnumerableExtensions.cs
License : MIT License
Project Creator : 17MKH
License : MIT License
Project Creator : 17MKH
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
19
View Source File : TaskHelpers.cs
License : MIT License
Project Creator : abdullin
License : MIT License
Project Creator : abdullin
public static Func<TSource, CancellationToken, TResult> WithCancellation<TSource, TResult>([NotNull] Func<TSource, TResult> lambda)
{
Contract.Requires(lambda != null);
return (value, ct) =>
{
if (ct.IsCancellationRequested) ct.ThrowIfCancellationRequested();
return lambda(value);
};
}
19
View Source File : TaskHelpers.cs
License : MIT License
Project Creator : abdullin
License : MIT License
Project Creator : abdullin
public static Func<TSource, CancellationToken, Task<TResult>> WithCancellation<TSource, TResult>([NotNull] Func<TSource, Task<TResult>> lambda)
{
Contract.Requires(lambda != null);
return (value, ct) =>
{
if (ct.IsCancellationRequested) return FromCancellation<TResult>(ct);
return lambda(value);
};
}
19
View Source File : All.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
if (_once)
{
return false;
}
_once = true;
while (await _source.MoveNextAsync())
{
if (!_predicate(_source.Current))
{
Current = false;
return true;
}
}
Current = true;
return true;
}
19
View Source File : All.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
if (_once)
{
return false;
}
_once = true;
while (await _source.MoveNextAsync())
{
if (!await _predicate(_source.Current))
{
Current = false;
return true;
}
}
Current = true;
return true;
}
19
View Source File : Any.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
if (_once)
{
return false;
}
_once = true;
while (await _source.MoveNextAsync())
{
if (_predicate(_source.Current))
{
Current = true;
return true;
}
}
Current = false;
return true;
}
19
View Source File : Any.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
if (_once)
{
return false;
}
_once = true;
while (await _source.MoveNextAsync())
{
if (await _predicate(_source.Current))
{
Current = true;
return true;
}
}
Current = false;
return true;
}
19
View Source File : ConcatMap.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
Current = default;
for (; ;)
{
if (_inner == null)
{
if (await _source.MoveNextAsync())
{
_inner = _mapper(_source.Current).GetAsyncEnumerator(_ct);
}
else
{
return false;
}
}
if (await _inner.MoveNextAsync())
{
Current = _inner.Current;
return true;
}
await _inner.DisposeAsync();
_inner = null;
}
}
19
View Source File : ConcatMap.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
Current = default;
for (; ; )
{
if (_inner == null)
{
if (await _source.MoveNextAsync())
{
_inner = _mapper(_source.Current).GetEnumerator();
}
else
{
return false;
}
}
if (!_ct.IsCancellationRequested && _inner.MoveNext())
{
Current = _inner.Current;
return true;
}
_inner.Dispose();
_inner = null;
}
}
19
View Source File : ConcatMapEager.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void NextHandler(Task<bool> t)
{
if (t.IsCanceled)
{
ExceptionHelper.AddException(ref _error, new OperationCanceledException());
_sourceDone = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
}
} else
if (t.IsFaulted)
{
ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
_sourceDone = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
}
}
else if (t.Result)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(_sourceCTS.Token);
IAsyncEnumerator<TResult> src;
try
{
src = _mapper(_source.Current).GetAsyncEnumerator(cts.Token);
}
catch (Exception ex)
{
ExceptionHelper.AddException(ref _error, ex);
_sourceDone = true;
src = null;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
return;
}
}
if (src != null)
{
Interlocked.Increment(ref _disposeWip);
var inner = new InnerHandler(src, this, cts);
_inners.Enqueue(inner);
if (_disposeRequested)
{
while (_inners.TryDequeue(out var inner2))
{
inner2.Dispose();
}
}
if (TryDispose())
{
inner.MoveNext();
if (Interlocked.Decrement(ref _sourceOutstanding) != 0)
{
MoveNextSource();
}
ResumeHelper.Resume(ref _resume);
}
}
}
else
{
_sourceDone = true;
if (TryDispose())
{
ResumeHelper.Resume(ref _resume);
}
}
}
19
View Source File : Distinct.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
for (; ; )
{
if (await _source.MoveNextAsync())
{
if (_collection.Add(_keySelector(_source.Current)))
{
return true;
}
}
else
{
return false;
}
}
}
19
View Source File : DistinctUntilChanged.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
for (; ;)
{
if (await _source.MoveNextAsync())
{
var key = _keySelector(_source.Current);
if (!_once)
{
_once = true;
_prevKey = key;
return true;
}
if (!_keyComparer.Equals(_prevKey, key))
{
_prevKey = key;
return true;
}
_prevKey = key;
}
else
{
return false;
}
}
}
19
View Source File : FlatMap.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void Handle(Task<bool> task)
{
if (task.IsCanceled)
{
AddException(new OperationCanceledException());
_done = true;
if (TryDispose())
{
Signal();
}
}
else if (task.IsFaulted)
{
AddException(task.Exception);
_done = true;
if (TryDispose())
{
Signal();
}
}
else
{
if (task.Result)
{
var v = _source.Current;
if (TryDispose())
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(_sourceCTS.Token);
IAsyncEnumerator<TResult> innerSource;
try
{
innerSource = _mapper(v)
.GetAsyncEnumerator(cts.Token);
}
catch (Exception ex)
{
_source.DisposeAsync();
AddException(ex);
_done = true;
Signal();
return;
}
var handler = new InnerHandler(this, innerSource, _prefetch, cts);
Interlocked.Increment(ref _allDisposeWip);
if (Add(handler))
{
handler.MoveNext();
if (Interlocked.Decrement(ref _outstanding) != 0)
{
MoveNext();
}
}
else
{
// This will decrement _allDisposeWip so
// that the DisposeAsync() can be released eventually
DisposeOne();
}
}
}
else
{
_done = true;
if (TryDispose())
{
Signal();
}
}
}
}
19
View Source File : GroupBy.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask DisposeAsync()
{
if (!_done)
{
for (; ; )
{
try
{
if (_groups.IsEmpty)
{
break;
}
if (await _source.MoveNextAsync())
{
var t = _source.Current;
var k = _keySelector(t);
var found = _groups.TryGetValue(k, out var g);
if (found)
{
await g.Next(_valueSelector(t));
}
}
else
{
foreach (var gr in _groups)
{
await gr.Value.Complete();
}
break;
}
}
catch (Exception ex)
{
foreach (var gr in _groups)
{
await gr.Value.Error(ex);
}
throw;
}
}
}
if (Interlocked.Decrement(ref _active) == 0)
{
await _source.DisposeAsync();
}
}
19
View Source File : GroupBy.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
for (; ;)
{
try
{
if (await _source.MoveNextAsync())
{
var t = _source.Current;
var k = _keySelector(t);
var found = _groups.TryGetValue(k, out var g);
if (!found)
{
g = new Group(k, this);
Interlocked.Increment(ref _active);
_groups.TryAdd(k, g);
Current = g;
}
await g.Next(_valueSelector(t));
if (!found)
{
return true;
}
}
else
{
foreach (var gr in _groups)
{
await gr.Value.Complete();
}
_done = true;
return false;
}
}
catch (Exception ex)
{
foreach (var gr in _groups)
{
await gr.Value.Error(ex);
}
_done = true;
throw;
}
}
}
19
View Source File : Map.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
if (await _source.MoveNextAsync())
{
Current = _mapper(_source.Current);
return true;
}
return false;
}
19
View Source File : Map.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
public async ValueTask<bool> MoveNextAsync()
{
if (await _source.MoveNextAsync())
{
Current = await _mapper(_source.Current);
return true;
}
return false;
}
19
View Source File : SwitchMap.cs
License : Apache License 2.0
Project Creator : akarnokd
License : Apache License 2.0
Project Creator : akarnokd
private void NextHandler(Task<bool> t)
{
if (t.IsCanceled)
{
ExceptionHelper.AddException(ref _error, new OperationCanceledException());
_done = true;
if (TryDispose())
{
Signal();
}
}
else if (t.IsFaulted)
{
ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
_done = true;
if (TryDispose())
{
Signal();
}
}
else if (t.Result)
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(_sourceCTS.Token);
IAsyncEnumerator<TResult> src;
try
{
src = _mapper(_source.Current).GetAsyncEnumerator(cts.Token);
}
catch (Exception ex)
{
ExceptionHelper.AddException(ref _error, ex);
_done = true;
Dispose(_source);
Signal();
return;
}
if (TryDispose())
{
Interlocked.Increment(ref _allDisposeWip);
var inner = new InnerHandler(src, this, cts);
for (; ; )
{
var curr = Volatile.Read(ref _current);
if (curr == DisposedInnerHandler)
{
inner.Dispose();
break;
}
if (Interlocked.CompareExchange(ref _current, inner, curr) == curr)
{
curr?.Dispose();
inner.MoveNext();
MoveNext();
return;
}
}
}
}
else
{
_done = true;
if (TryDispose())
{
Signal();
}
}
}
19
View Source File : StringUtils.cs
License : MIT License
Project Creator : akaskela
License : MIT License
Project Creator : akaskela
public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> valueSelector, string testValue)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (valueSelector == null)
{
throw new ArgumentNullException(nameof(valueSelector));
}
var caseInsensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase));
if (caseInsensitiveResults.Count() <= 1)
{
return caseInsensitiveResults.SingleOrDefault();
}
else
{
// multiple results returned. now filter using case sensitivity
var caseSensitiveResults = source.Where(s => string.Equals(valueSelector(s), testValue, StringComparison.Ordinal));
return caseSensitiveResults.SingleOrDefault();
}
}
19
View Source File : Extensions.cs
License : MIT License
Project Creator : AkiniKites
License : MIT License
Project Creator : AkiniKites
public static Dictionary<TKey, TValue> ToSoftDictionary<TSource, TKey, TValue>(
this IEnumerable<TSource> source, Func<TSource, TKey> key, Func<TSource, TValue> value)
{
var dict = new Dictionary<TKey, TValue>();
foreach (var item in source)
dict[key(item)] = value(item);
return dict;
}
19
View Source File : EnumerableExtensions.cs
License : GNU Affero General Public License v3.0
Project Creator : akira0245
License : GNU Affero General Public License v3.0
Project Creator : akira0245
public static TSource MaxElement<TSource, R>(this IEnumerable<TSource> container, Func<TSource, R> valuingFoo) where R : IComparable
{
var enumerator = container.GetEnumerator();
if (!enumerator.MoveNext())
throw new ArgumentException("Container is empty!");
var maxElem = enumerator.Current;
var maxVal = valuingFoo(maxElem);
while (enumerator.MoveNext())
{
var currVal = valuingFoo(enumerator.Current);
if (currVal.CompareTo(maxVal) > 0)
{
maxVal = currVal;
maxElem = enumerator.Current;
}
}
return maxElem;
}
19
View Source File : EnumerableExtensions.cs
License : GNU Affero General Public License v3.0
Project Creator : akira0245
License : GNU Affero General Public License v3.0
Project Creator : akira0245
public static TSource MinElement<TSource, R>(this IEnumerable<TSource> container, Func<TSource, R> valuingFoo) where R : IComparable
{
var enumerator = container.GetEnumerator();
if (!enumerator.MoveNext())
throw new ArgumentException("Container is empty!");
var maxElem = enumerator.Current;
var maxVal = valuingFoo(maxElem);
while (enumerator.MoveNext())
{
var currVal = valuingFoo(enumerator.Current);
if (currVal.CompareTo(maxVal) < 0)
{
maxVal = currVal;
maxElem = enumerator.Current;
}
}
return maxElem;
}
19
View Source File : IEnumerableExtensions.cs
License : MIT License
Project Creator : AlphaYu
License : MIT License
Project Creator : AlphaYu
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var hash = new HashSet<TKey>();
return source.Where(p => hash.Add(keySelector(p)));
}
19
View Source File : AsyncEnumerableExtensions.cs
License : MIT License
Project Creator : Aminator
License : MIT License
Project Creator : Aminator
public static async ValueTask<int> CountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
{
int count = 0;
await foreach (TSource element in source)
{
checked
{
if (predicate(element))
{
count++;
}
}
}
return count;
}
19
View Source File : AsyncEnumerableExtensions.cs
License : MIT License
Project Creator : Aminator
License : MIT License
Project Creator : Aminator
public static async ValueTask<long> LongCountAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
{
long count = 0;
await foreach (TSource element in source)
{
checked
{
if (predicate(element))
{
count++;
}
}
}
return count;
}
19
View Source File : IEnumerableExtensions.cs
License : MIT License
Project Creator : amwx
License : MIT License
Project Creator : amwx
public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
19
View Source File : Memorized.cs
License : Apache License 2.0
Project Creator : Anapher
License : Apache License 2.0
Project Creator : Anapher
public static Func<TSource, ValueTask<TReturn>> Func<TSource, TReturn>(Func<TSource, ValueTask<TReturn>> func)
{
KeyValuePair<TSource, TReturn>? cache = null;
var @lock = new AsyncLock();
return async s =>
{
var tmpCache = cache;
if (tmpCache.HasValue && Equals(tmpCache.Value.Key, s))
return tmpCache.Value.Value;
using (await @lock.LockAsync())
{
if (!cache.HasValue || !Equals(cache.Value.Key, s))
cache = new KeyValuePair<TSource, TReturn>(s, await func(s));
return cache.Value.Value;
}
};
}
19
View Source File : LinqExtensions.cs
License : MIT License
Project Creator : anet-team
License : MIT License
Project Creator : anet-team
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> knownKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (knownKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
19
View Source File : DynamicLinqFactory.cs
License : Apache License 2.0
Project Creator : anjoy8
License : Apache License 2.0
Project Creator : anjoy8
public static IEnumerable<TSource> IDistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var seenKeys = new HashSet<TKey>();
return source.Where(element => seenKeys.Add(keySelector(element)));
}
19
View Source File : LinqSelectExtensions.cs
License : MIT License
Project Creator : anjoy8
License : MIT License
Project Creator : anjoy8
public static IEnumerable<SelectTryResult<TSource, TResult>> SelectTry<TSource, TResult>(this IEnumerable<TSource> enumerable, Func<TSource, TResult> selector)
{
foreach (TSource element in enumerable)
{
SelectTryResult<TSource, TResult> returnedValue;
try
{
returnedValue = new SelectTryResult<TSource, TResult>(element, selector(element), null);
}
catch (Exception ex)
{
returnedValue = new SelectTryResult<TSource, TResult>(element, default(TResult), ex);
}
yield return returnedValue;
}
}
19
View Source File : IEnumerableExtensions.cs
License : MIT License
Project Creator : AnnoDesigner
License : MIT License
Project Creator : AnnoDesigner
public static Dictionary<TKey, TSource> ToDictionaryWithCapacity<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
int capacity;
if (source is ICollection<TSource> collection)
{
capacity = collection.Count;
}
else if (source is IReadOnlyCollection<TSource> readonlyCollection)
{
capacity = readonlyCollection.Count;
}
else
{
capacity = source.Count();
}
var result = new Dictionary<TKey, TSource>(capacity);
foreach (var current in source)
{
result.Add(keySelector(current), current);
}
return result;
}
19
View Source File : LinqExtension.cs
License : MIT License
Project Creator : appsonsf
License : MIT License
Project Creator : appsonsf
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
19
View Source File : EnumerableExtensions.cs
License : GNU General Public License v3.0
Project Creator : Artentus
License : GNU General Public License v3.0
Project Creator : Artentus
public static TSource? MaxBy<TSource, TKey>(this IEnumerable<TSource> source, in Func<TSource, TKey> selector, IComparer<TKey>? comparer = null)
{
if (source is null) throw new ArgumentNullException(nameof(source));
if (selector is null) throw new ArgumentNullException(nameof(selector));
if (comparer is null) comparer = Comparer<TKey>.Default;
using var enumerator = source.GetEnumerator();
if (!enumerator.MoveNext()) return default;
TSource maxElement = enumerator.Current;
TKey maxKey = selector.Invoke(maxElement);
while (enumerator.MoveNext())
{
TSource element = enumerator.Current;
TKey key = selector.Invoke(element);
if (comparer.Compare(key, maxKey) > 0)
{
maxElement = element;
maxKey = key;
}
}
return maxElement;
}
19
View Source File : EnumerableExtensions.cs
License : GNU General Public License v3.0
Project Creator : Artentus
License : GNU General Public License v3.0
Project Creator : Artentus
public static TSource? MinBy<TSource, TKey>(this IEnumerable<TSource> source, in Func<TSource, TKey> selector, IComparer<TKey>? comparer = null)
{
if (source is null) throw new ArgumentNullException(nameof(source));
if (selector is null) throw new ArgumentNullException(nameof(selector));
if (comparer is null) comparer = Comparer<TKey>.Default;
using var enumerator = source.GetEnumerator();
if (!enumerator.MoveNext()) return default;
TSource maxElement = enumerator.Current;
TKey maxKey = selector.Invoke(maxElement);
while (enumerator.MoveNext())
{
TSource element = enumerator.Current;
TKey key = selector.Invoke(element);
if (comparer.Compare(key, maxKey) < 0)
{
maxElement = element;
maxKey = key;
}
}
return maxElement;
}
19
View Source File : EnumerableExtensions.cs
License : GNU General Public License v3.0
Project Creator : Artentus
License : GNU General Public License v3.0
Project Creator : Artentus
public static TResult? SelectFromAll<TSource, TResult>(this IEnumerable<TSource> source, in Func<TSource, TResult> selector, IEqualityComparer<TResult>? comparer = null)
{
if (source is null) throw new ArgumentNullException(nameof(source));
if (selector is null) throw new ArgumentNullException(nameof(selector));
if (comparer is null) comparer = EqualityComparer<TResult>.Default;
using var enumerator = source.GetEnumerator();
if (!enumerator.MoveNext()) return default;
TResult result = selector(enumerator.Current);
while (enumerator.MoveNext())
{
TResult current = selector(enumerator.Current);
if (!comparer.Equals(result, current)) return default;
}
return result;
}
19
View Source File : ListExtensions.cs
License : Apache License 2.0
Project Creator : AutomateThePlanet
License : Apache License 2.0
Project Creator : AutomateThePlanet
public static int IndexOf<TSource>(this IList<TSource> source, Func<TSource, bool> predicate)
where TSource : IEquatable<TSource>
{
for (int i = 0; i < source.Count; i++)
{
if (predicate(source[i]))
{
return i;
}
}
throw new ArgumentException("List doesn't contain such an element");
}
19
View Source File : CollectionExtensions.cs
License : MIT License
Project Creator : ay2015
License : MIT License
Project Creator : ay2015
[Pure]
public static int IndexOf<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
Contract.Requires(source != null);
Contract.Requires(predicate != null);
int index = 0;
foreach (TSource item in source)
{
if (predicate(item))
{
return index;
}
index++;
}
return -1;
}
19
View Source File : CollectionExtensions.cs
License : MIT License
Project Creator : ay2015
License : MIT License
Project Creator : ay2015
public static void Synchronize<TSource, TTarget>(this ObservableCollectionPlus<TTarget> targetCollection, IList<TSource> sourceCollection, Func<TSource, TTarget, bool> matcher, Func<TSource, TTarget> mapper) where TSource : IEquatable<TSource>
{
Contract.Requires(targetCollection != null);
Contract.Requires(mapper != null);
Contract.Requires(sourceCollection != null);
Contract.Requires(sourceCollection.AllUnique());
using (targetCollection.BeginMultiUpdate())
{
// move wrappers around to the right places
// or create a new one
for (int i = 0; i < sourceCollection.Count; i++)
{
var sourceItem = sourceCollection[i];
var targetIndex = targetCollection.IndexOf(targereplacedem => matcher(sourceItem, targereplacedem));
if (targetIndex >= 0)
{
if (targetIndex != i)
{
Debug.replacedert(targetIndex > i, "this would only happen if we have duplicates...which we should never have!");
targetCollection.Move(targetIndex, i);
}
else
{
// NOOP - already in the right spot! :-)
}
}
else
{
var newItem = mapper(sourceItem);
Debug.replacedert(matcher(sourceItem, newItem));
targetCollection.Insert(i, newItem);
}
}
// Remove anything left
while (targetCollection.Count > sourceCollection.Count)
{
targetCollection.RemoveLast();
}
Debug.replacedert(sourceCollection.Count == targetCollection.Count);
#if DEBUG
for (int i = 0; i < sourceCollection.Count; i++)
{
Debug.replacedert(matcher(sourceCollection[i], targetCollection[i]));
}
#endif
}
}
19
View Source File : AyCommon.cs
License : MIT License
Project Creator : ay2015
License : MIT License
Project Creator : ay2015
[Pure]
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
19
View Source File : KeyEqualityComparer.cs
License : GNU General Public License v3.0
Project Creator : az64
License : GNU General Public License v3.0
Project Creator : az64
public bool Equals(TSource x, TSource y)
{
return _comparer.Equals(_keySelector(x), _keySelector(y));
}
19
View Source File : KeyEqualityComparer.cs
License : GNU General Public License v3.0
Project Creator : az64
License : GNU General Public License v3.0
Project Creator : az64
public int GetHashCode(TSource obj)
{
return _comparer.GetHashCode(_keySelector(obj));
}
19
View Source File : ArrayExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static void Sort<TSource, TResult>( this TSource[] array, Func<TSource, TResult> selector ) where TResult : IComparable
{
Array.Sort( array, ( x, y ) => selector( x ).CompareTo( selector( y ) ) );
}
19
View Source File : ArrayExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static void SortDescending<TSource, TResult>( this TSource[] array, Func<TSource, TResult> selector ) where TResult : IComparable
{
Array.Sort( array, ( x, y ) => selector( y ).CompareTo( selector( x ) ) );
}
19
View Source File : ArrayExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static void Sort<TSource, TResult>( this TSource[] array, Func<TSource, TResult> selector1, Func<TSource, TResult> selector2 ) where TResult : IComparable
{
Array.Sort( array, ( x, y ) =>
{
var result = selector1( x ).CompareTo( selector1( y ) );
return result != 0 ? result : selector2( x ).CompareTo( selector2( y ) );
} );
}
19
View Source File : IEnumerableExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static TSource MaxBy<TSource, TResult>
(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector
)
{
var value = source.Max( selector );
return source.FirstOrDefault( c => selector( c ).Equals( value ) );
}
19
View Source File : IEnumerableExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static IEnumerable<TSource> MaxElementsBy<TSource, TResult>
(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector
)
{
var value = source.Max( selector );
return source.Where( c => selector( c ).Equals( value ) );
}
19
View Source File : IEnumerableExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static TSource MinBy<TSource, TResult>
(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector
)
{
var value = source.Min( selector );
return source.FirstOrDefault( c => selector( c ).Equals( value ) );
}
19
View Source File : IEnumerableExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static IEnumerable<TSource> MinElementsBy<TSource, TResult>
(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector
)
{
var value = source.Min( selector );
return source.Where( c => selector( c ).Equals( value ) );
}
19
View Source File : IEnumerableExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static IEnumerable<TSource> WhereNot<TSource, TResult>
(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
{
return source.Where( c => !predicate( c ) );
}
19
View Source File : IEnumerableExt.cs
License : MIT License
Project Creator : baba-s
License : MIT License
Project Creator : baba-s
public static int Nearest<TSource>
(
this IEnumerable<TSource> self,
int target,
Func<TSource, int> selector
)
{
var min = self.Min( c => Math.Abs( selector( c ) - target ) );
return selector( self.FirstOrDefault( c => Math.Abs( selector( c ) - target ) == min ) );
}
See More Examples