Created
April 24, 2015 14:00
-
-
Save lunaris/823e2218ad3aef52c357 to your computer and use it in GitHub Desktop.
C# Maybe Sketch
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
public abstract class Maybe<A> { | |
public abstract A Value { get; } | |
public abstract Maybe<B> Map<B>(Func<A, B> f); | |
private Maybe() { } | |
public static Maybe<A> nothing() { | |
return new Nothing(); | |
} | |
public static Maybe<A> just(A value) { | |
return new Just(value); | |
} | |
private class Nothing<A> : Maybe<A> { | |
private Nothing() : base() {} | |
public override A Value { | |
get { throw new Exception(); } | |
} | |
public override Maybe<B> Map<B>(Func<A, B> f) { | |
return nothing(); | |
} | |
} | |
private class Just<A> : Maybe<A> { | |
private readonly A _value; | |
private Just(A value) : base() { | |
_value = value; | |
} | |
public override A Value { | |
get { return _value; } | |
} | |
public override Maybe<B> Map<B>(Func<A, B> f) { | |
return just(f(_value)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment