Created
September 29, 2016 20:39
-
-
Save boca/c80c58385a6f733de4ded4dd6a692918 to your computer and use it in GitHub Desktop.
Monads!
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
void Main() | |
{ | |
"Hello".Unit().Dump(); | |
5.Unit().Dump(); | |
DateTime.Today.Unit().Dump(); | |
new List<char> { 'a', 'b' }.Unit().Dump(); | |
(3 * 1.5).Unit().Dump(); | |
} | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> Unit<T>(this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U>(this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
} |
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
void Main() | |
{ | |
4.Unit() | |
.Bind(x => (x * 2.5).Unit()) | |
.Dump(); | |
} | |
// Define other methods and classes here | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> Unit<T>(this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U>(this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
} |
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
void Main() | |
{ | |
4.Unit() | |
.Bind(x => (x * 2.5).Unit()) | |
.Dump(); | |
} | |
// Define other methods and classes here | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> Unit<T>(this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U>(this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
} |
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
void Main() | |
{ | |
} | |
// Define other methods and classes here | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> ToIdentity<T> (this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U> (this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
public static Identity<V> SelectMany<T,U,V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T,U,V> s) => s(id.Value, k(id.Value).Value).ToIdentity(); | |
} |
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
void Main() | |
{ | |
5.ToIdentity() | |
.SelectMany(x => 9.ToIdentity(), (x,y) => (x+y)) | |
.Dump(); | |
} | |
// Define other methods and classes here | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> ToIdentity<T> (this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U> (this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
public static Identity<V> SelectMany<T,U,V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T,U,V> s) => s(id.Value, k(id.Value).Value).ToIdentity(); | |
} |
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
void Main() | |
{ | |
( | |
from x in 5.ToIdentity() | |
from y in 9.ToIdentity() | |
select (x + y) | |
).Dump(); | |
} | |
// Define other methods and classes here | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> ToIdentity<T> (this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U> (this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
public static Identity<V> SelectMany<T,U,V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T,U,V> s) => s(id.Value, k(id.Value).Value).ToIdentity(); | |
} |
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
void Main() | |
{ | |
( | |
from x in new[] { 1, 2 } | |
from y in new List<int> { 3, 4, 5} | |
select (x + y) | |
).Dump(); | |
} | |
// Define other methods and classes here | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> ToIdentity<T> (this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U> (this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
public static Identity<V> SelectMany<T,U,V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T,U,V> s) => s(id.Value, k(id.Value).Value).ToIdentity(); | |
} |
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
void Main() | |
{ | |
} | |
// Define other methods and classes here | |
public class Maybe<T> | |
{ | |
public readonly static Maybe<T> Nothing = new Maybe<T>(); | |
public T Value { get; } | |
public bool HasValue { get;} | |
public Maybe() {HasValue=false;} | |
public Maybe(T value) { Value = value; HasValue=true;} | |
} | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> ToIdentity<T> (this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U> (this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
public static Identity<V> SelectMany<T,U,V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T,U,V> s) => s(id.Value, k(id.Value).Value).ToIdentity(); | |
public static Maybe<T> ToMaybe<T> (this T m) => new Maybe<T>(m); | |
public static Maybe<U> Bind<T,U> (this Maybe<T> m, Func<T,Maybe<U>> k) => m.HasValue ? k(m.Value) : Maybe<U>.Nothing; | |
public static Maybe<V> SelectMany<T,U,V> (this Maybe<T> m, Func<T,Maybe<U>> k, Func<T,U,V> s) => k(m.Value).HasValue ? s(m.Value, k(m.Value).Value).ToMaybe() : Maybe<V>.Nothing | |
} |
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
void Main() | |
{ | |
( | |
from x in 5.ToMaybe() | |
from y in 9.ToMaybe() | |
select x + y | |
).Dump(); | |
( | |
from x in Maybe<int>.Nothing | |
from y in 9.ToMaybe() | |
select x + y | |
).Dump(); | |
( | |
from x in 5.ToMaybe() | |
from y in Maybe<int>.Nothing | |
select x + y | |
).Dump(); | |
( | |
from x in 5.ToMaybe() | |
from y in 9.ToMaybe() | |
from z in new Maybe<int>() | |
select x + y + z | |
).Dump(); | |
} | |
// Define other methods and classes here | |
public class Maybe<T> | |
{ | |
public readonly static Maybe<T> Nothing = new Maybe<T>(); | |
public T Value { get; } | |
public bool HasValue { get;} | |
public Maybe() {HasValue=false;} | |
public Maybe(T value) { Value = value; HasValue=true;} | |
} | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> ToIdentity<T> (this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U> (this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
public static Identity<V> SelectMany<T,U,V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T,U,V> s) => s(id.Value, k(id.Value).Value).ToIdentity(); | |
public static Maybe<T> ToMaybe<T> (this T m) => new Maybe<T>(m); | |
public static Maybe<U> Bind<T,U> (this Maybe<T> m, Func<T,Maybe<U>> k) => m.HasValue ? k(m.Value) : Maybe<U>.Nothing; | |
public static Maybe<V> SelectMany<T,U,V> (this Maybe<T> m, Func<T,Maybe<U>> k, Func<T,U,V> s) => m.Bind(k).HasValue ? s(m.Value, m.Bind(k).Value).ToMaybe() : Maybe<V>.Nothing; | |
} |
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
void Main() | |
{ | |
( | |
from x in 5.ToMaybe() | |
from y in 9.ToMaybe() | |
select x + y | |
).Dump(); | |
( | |
from x in DateTime.Now.Millisecond.ToMaybe() | |
from y in ExcludePairs(x) | |
from z in Times2(y).ToMaybe() | |
select x+y+z | |
).Dump(); | |
} | |
// Define other methods and classes here | |
public static int Times2(int x) => x * 2; | |
public static Maybe<int> ExcludePairs(int x) => x % 2 == 0 ? Maybe<int>.Nothing : x.ToMaybe(); | |
public class Maybe<T> | |
{ | |
public readonly static Maybe<T> Nothing = new Maybe<T>(); | |
public T Value { get; } | |
public bool HasValue { get;} | |
public Maybe() {HasValue=false;} | |
public Maybe(T value) { Value = value; HasValue=true;} | |
} | |
public class Identity<T> | |
{ | |
public T Value {get;} | |
public Identity(T value) {Value=value;} | |
} | |
public static class MonadHelper | |
{ | |
public static Identity<T> ToIdentity<T> (this T t) => new Identity<T>(t); | |
public static Identity<U> Bind<T,U> (this Identity<T> id, Func<T, Identity<U>> k) => k(id.Value); | |
public static Identity<V> SelectMany<T,U,V>(this Identity<T> id, Func<T, Identity<U>> k, Func<T,U,V> s) => s(id.Value, k(id.Value).Value).ToIdentity(); | |
public static Maybe<T> ToMaybe<T> (this T m) => new Maybe<T>(m); | |
public static Maybe<U> Bind<T,U> (this Maybe<T> m, Func<T,Maybe<U>> k) => m.HasValue ? k(m.Value) : Maybe<U>.Nothing; | |
public static Maybe<V> SelectMany<T,U,V> (this Maybe<T> m, Func<T,Maybe<U>> k, Func<T,U,V> s) => m.Bind(k).HasValue ? s(m.Value, m.Bind(k).Value).ToMaybe() : Maybe<V>.Nothing; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment