Skip to content

Instantly share code, notes, and snippets.

@Warry
Last active August 29, 2015 14:05
Show Gist options
  • Save Warry/1472519278124015ac7d to your computer and use it in GitHub Desktop.
Save Warry/1472519278124015ac7d to your computer and use it in GitHub Desktop.
this compiles !???
interface Functor<A>{
flatMap<A,B> (f: (A) => B): Functor<B>
}
interface Monad<A> extends Functor<A> {
bind: (A) => Monad<A>
}
interface Option<A> extends Monad<A>{
}
class Some<A> implements Option<A> {
private value: A
constructor(a: A) {
this.value = a
}
flatMap<A,B> (f: (A) => B): Some<B>{
return new Some(f(this.value))
}
bind (f: (A) => Some<A>): Some<A>{
return f(this.value)
}
}
class None<A> implements Option<A>{
flatMap<A,B> (f: (A) => B): None<B> {
return new None<B>()
}
bind (f: (A) => None<A>): None<A>{
return this
}
}
var test:Option<string> = new Some(4)
test = new None()
test = new Some(false) // ! \o/ this compiles...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment