Skip to content

Instantly share code, notes, and snippets.

View idg10's full-sized avatar

Ian Griffiths idg10

  • endjin
  • Hove, UK
View GitHub Profile
@idg10
idg10 / BenchmarkAsyncSwitchBlocking.cs
Created June 7, 2023 09:57
Benchmarking two different approaches to IAsyncEnumerable.Switch, forcing non-synchronous ValueTask completion
[MemoryDiagnoser]
public class SwitchWithBlockingSources
{
public readonly static IAsyncEnumerable<IAsyncEnumerable<int>> source = AsyncEnumerable
.Range(0, 1000)
.SelectAwait(async i =>
{
await Task.Yield();
return AsyncEnumerable
.Range(0, 1000)
@idg10
idg10 / BenchmarkAsyncSwitch.cs
Created June 7, 2023 09:48
Benchmarking two different approaches to IAsyncEnumerable.Switch
[MemoryDiagnoser]
public class SwitchWithNonBlockingSources
{
public readonly static IAsyncEnumerable<IAsyncEnumerable<int>> source = AsyncEnumerable
.Range(0, 1000)
.Select(i => AsyncEnumerable.Range(0, 1000));
[Benchmark]
public async ValueTask<int> IterativeSingleLogicalThread()
{
@idg10
idg10 / SwitchExtension.cs
Last active June 7, 2023 14:07
A spike illustrating one possible implementation strategy for an `IAsyncEnumerable` `Switch` operator
namespace AsyncEnumerableSwitchSpike;
internal static class SwitchExtension
{
public static async IAsyncEnumerable<T> Switch<T>(this IAsyncEnumerable<IAsyncEnumerable<T>> sequences)
{
IAsyncEnumerator<IAsyncEnumerable<T>>? currentOuterEnumerator = sequences.GetAsyncEnumerator();
IAsyncEnumerator<T>? currentInnerEnumerator = null;
try
@idg10
idg10 / LinqyFibonnaci.cs
Last active October 6, 2022 08:49
LINQy C# Fibonnaci
// Written as a reply to https://twitter.com/buhakmeh/status/1577738711013335040
// (C) 2022 I D Griffiths
// EnumerableEx from System.Interactive
IEnumerable<BigInteger> FibonnaciEndless() => EnumerableEx.Generate(
(i: new BigInteger(0), n: new BigInteger(1)),
_ => true,
s => (s.n, s.i + s.n),
s => s.i);
@idg10
idg10 / IdiomaticFibonacci.cs
Created October 6, 2022 08:48
Idiomatic C# Fibonnaci
// Written as a reply to https://twitter.com/buhakmeh/status/1577738711013335040
// (C) 2022 I D Griffiths
IEnumerable<BigInteger> FibonnaciEndless()
{
BigInteger i = 0, n = 1;
while (true)
{
yield return i;
(i, n) = (n, i + n);
@idg10
idg10 / BadEmptyStringInitialization.cs
Created September 30, 2020 12:15
A bad way to make a CS8618 warning go away
public class Person
{
// This gets rid of the CS8618 warning, but it is
// A BAD IDEA!
public string FavouriteColour { get; set; } = "";
}
@idg10
idg10 / CastNotAs.cs
Last active August 26, 2020 15:37
Expressing the expectation of success with a cast
var o = (JObject)JToken.Parse(text);
bool hasItem = o.ContainsKey("item");
@idg10
idg10 / PatternsInsteadOfAs.cs
Last active August 26, 2020 15:28
Using patterns instead of as
if (JToken.Parse(text) is JObject o)
{
bool hasItem = o.ContainsKey("item");
Console.WriteLine(hasItem);
}
@idg10
idg10 / OkUsesOfAs.cs
Last active August 26, 2020 15:27
Some reasonable uses of as
var o = JToken.Parse(text) as JObject;
if (o != null)
{
Console.WriteLine("item is " + (o.ContainsKey("item") ? "present" : "absent"));
}
bool hasItem = o != null ? o.ContainsKey("item") : false;
bool? itemPresence = o?.ContainsKey("item");
@idg10
idg10 / BadUseOfAs.cs
Last active August 26, 2020 15:25
A common misuse of C#'s as operator
var o = JToken.Parse(text) as JObject;
bool hasItem = o.ContainsKey("item");