Created
January 2, 2026 06:32
-
-
Save altbodhi/c029dc7f434e6ab363841f5066477651 to your computer and use it in GitHub Desktop.
Simplest monad via C#
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
| 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