Created
October 28, 2019 20:08
-
-
Save pictos/29e99b98676f9eb88da74c28635299a2 to your computer and use it in GitHub Desktop.
Exemple of features of C#8 (It's not mine)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IProcessor<T,U> where T: unmanaged, IResult | |
{ | |
T ProcessSingle(int x); | |
async IAsyncEnumerable<T> ProcessMany(int[] xs, int batch) | |
{ | |
static T[] ProccessBatch(Func<int,T> func, Span<int> list, in Range range) | |
{ | |
(_, int length) = range.GetOffsetAndLenght(list.length); | |
using var batcher = new Batcher<T>(stackalloc T[length]); | |
return batcher.Proccess(func, list[range]); | |
} | |
for(int i = 0; i < xs.Length; i += batch) | |
{ | |
foreach (var t in ProccessBatch(ProccessSingle, xs, i..(i+batch))) | |
{ | |
yield return t switch | |
{ | |
{ IsSucceeded:false} => (T)IResult.Failed, | |
{ IsSucceeded:true, Description:null} => (T)IResult.Failed, | |
_ => await Task.FromResult(t) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment