Skip to content

Instantly share code, notes, and snippets.

@pictos
Created October 28, 2019 20:08
Show Gist options
  • Save pictos/29e99b98676f9eb88da74c28635299a2 to your computer and use it in GitHub Desktop.
Save pictos/29e99b98676f9eb88da74c28635299a2 to your computer and use it in GitHub Desktop.
Exemple of features of C#8 (It's not mine)
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