Created
May 8, 2017 09:16
-
-
Save abiodun0/a5bfe11f3fa3c129ae8e600f09c7b873 to your computer and use it in GitHub Desktop.
Monads in Typescript
This file contains hidden or 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
| interface Functor { | |
| fmap: (any) => any; | |
| } | |
| interface Monad extends Functor { | |
| bind: (any) => Monad; | |
| } | |
| interface Maybe extends Monad { | |
| } | |
| class Just implements Maybe { | |
| private value: any; | |
| constructor(a: any) { | |
| this.value = a | |
| } | |
| fmap (f: (any) => any): Just { | |
| return new Just(f(this.value)) | |
| } | |
| bind (f: (any) => Maybe): Maybe { | |
| return f(this.value) | |
| } | |
| } | |
| class Nothing implements Maybe { | |
| fmap (f: (any) => any): Nothing { | |
| return this | |
| } | |
| bind (f: (any) => Maybe): Maybe { | |
| return this | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment