Skip to content

Instantly share code, notes, and snippets.

@altbodhi
Created January 2, 2026 06:32
Show Gist options
  • Select an option

  • Save altbodhi/c029dc7f434e6ab363841f5066477651 to your computer and use it in GitHub Desktop.

Select an option

Save altbodhi/c029dc7f434e6ab363841f5066477651 to your computer and use it in GitHub Desktop.
Simplest monad via C#
var z = from x in 5.ToIdentity()
from y in 6.ToIdentity()
select x + y;
Console.WriteLine(z);
class Identity<T>
{
public T Value { get; private set; }
public Identity(T value) { this.Value = value; }
public override string ToString()
{
return $"[{nameof(Identity<>)}: {nameof(Value)} = {Value}]";
}
}
static class Ext
{
public static Identity<T> ToIdentity<T>(this T value)
{
return new Identity<T>(value);
}
public static Identity<V> SelectMany<T, U, V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T, U, V> s)
{
return s(id.Value, k(id.Value).Value).ToIdentity();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment