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) | |
| } |
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 Monad<A> { | |
| bind<B>(f: (a: A) => Monad<B>): Monad<B> | |
| return(a: A): Monad<A> | |
| } |
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
| console.log('mapped list:', [1, 2, 3].map(x => x * 2)) // [2, 4, 6] |
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 Functor<T> { | |
| private value: T | |
| constructor(value: T) { | |
| this.value = value | |
| } | |
| fmap<U>(f: (a: T) => U): Functor<U> { | |
| return new Container(f(this.value)) | |
| } |
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> { | |
| private value: T | |
| constructor(value: T) { | |
| this.value = value | |
| } | |
| } | |
| console.log('simple container:', new Container(5)) // Container { value: 5 } |
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<A> { | |
| fmap<B>(f: (a: A) => B): Functor<B> | |
| } |
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
| String.prototype.mappend = function (x: String): String { | |
| return this + x | |
| } | |
| String.prototype.mempty = function (): String { | |
| return "" | |
| } | |
| console.log( | |
| 'String mappend:', |
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
| Number.prototype.mappend = function (x: Number): Number { | |
| return this * x | |
| } | |
| Number.prototype.mempty = function (): Number { | |
| return 1 | |
| } | |
| console.log('mappend:', Number(5).mappend(4)) // 20 | |
| console.log('mempty:', Number().mempty()) // 1 |
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
| Number.prototype.mappend = function (x: Number): Number { | |
| return this + x | |
| } | |
| Number.prototype.mempty = function (): Number { | |
| return 0 | |
| } | |
| console.log('mappend:', Number(5).mappend(4)) // 9 | |
| console.log('mempty:', Number().mempty()) // 0 |
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 Monoid<M> { | |
| mappend(x: M): M // operação binária | |
| mempty(): M // elemento neutro | |
| } |