Created
November 28, 2015 08:53
-
-
Save aereal/d8c067040c7276201462 to your computer and use it in GitHub Desktop.
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
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