Last active
November 28, 2025 13:44
-
-
Save evaporei/723ca4a04877ef143d93dcd23878b71c to your computer and use it in GitHub Desktop.
Category Theory - Monad Container Implementation
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
| class Container<T> implements Monad<T> { | |
| private value: T | |
| constructor(value: T) { | |
| this.value = value | |
| } | |
| bind<U>(f: (a: T) => Monad<U>): Monad<U> { | |
| return f(this.value) | |
| } | |
| return(a: T): Monad<T> { | |
| return new Container(a) | |
| } | |
| } | |
| console.log( | |
| 'bind container with 5 to container with 15:', | |
| new Container(5).bind((a) => new Container(a + 10)) | |
| ) // Container { value: 15 } | |
| console.log( | |
| 'return:', | |
| new Container(undefined).return(6) | |
| ) // Container { value: 6 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment