Created
September 7, 2023 09:15
-
-
Save Savelenko/9254ee600a8b8a59b69410e2a8cbdd3b to your computer and use it in GitHub Desktop.
LINQ-to-nothing-special
This file contains 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
private static void Main(string[] args) | |
{ | |
/* LINQ-to-nothing-special */ | |
Just<int> five = 5; | |
Just<int> six = 6; | |
var eleven = from a in five | |
from b in six | |
select a + b; | |
Console.WriteLine(eleven); | |
var seven = from a in six | |
select Inc(a); | |
Console.WriteLine(seven); | |
Console.ReadKey(); | |
} | |
private static int Inc(int x) | |
{ | |
return x + 1; | |
} |
This file contains 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
/// A.k.a the Identity monad. | |
public struct Just<T> | |
{ | |
private readonly T value; | |
public Just(T value) | |
{ | |
this.value = value; | |
} | |
public static implicit operator T(Just<T> justValue) | |
{ | |
return justValue.value; | |
} | |
public static implicit operator Just<T>(T value) | |
{ | |
return new Just<T>(value); | |
} | |
public Just<U> Select<U>(Func<T, U> selector) | |
{ | |
return new Just<U>(selector(this.value)); | |
} | |
public Just<V> SelectMany<U, V>(Func<T, Just<U>> selector, Func<T, U, V> resultSelector) | |
{ | |
return resultSelector(this, selector(this)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment