Skip to content

Instantly share code, notes, and snippets.

@aereal
Created November 28, 2015 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aereal/d8c067040c7276201462 to your computer and use it in GitHub Desktop.
Save aereal/d8c067040c7276201462 to your computer and use it in GitHub Desktop.
abstract class M<A> {
abstract flatMap<B>(f: (a: A) => M<B>): M<B>;
}
abstract class MX<A> extends M<A> {
abstract flatMap<B>(f: (a: A) => MX<B>): MX<B>; // if omitted: error TS2339: Property 'flatMap' does not exist on type 'MX<A>'.
map<B>(f: (a: A) => B): MX<B> { return this.flatMap((a) => { return this.unit(f(a)) }) }
unit<A>(a: A): MX<A> { return new MX<A>(a) }
}
class N<A> extends MX<A> {
constructor(private value: A) {}
flatMap<B>(f: (a: A) => N<B>): N<B> { return f(this.value) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment