Skip to content

Instantly share code, notes, and snippets.

@lunaris
Created April 24, 2015 14:00
Show Gist options
  • Save lunaris/823e2218ad3aef52c357 to your computer and use it in GitHub Desktop.
Save lunaris/823e2218ad3aef52c357 to your computer and use it in GitHub Desktop.
C# Maybe Sketch
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