Last active
August 29, 2022 03:19
Revisions
-
venil7 revised this gist
Apr 6, 2018 . 1 changed file with 20 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,29 @@ class ReaderMonad<A, B> { constructor(public runReader: (a: A) => B) { } bind<C>(func: (b: B) => ReaderMonad<A, C>): ReaderMonad<A, C> { return new ReaderMonad<A, C>((a: A) => { const b = this.runReader(a); return func(b).runReader(a); }); } static return<A, B>(b: B): ReaderMonad<A, B> { return new ReaderMonad((a: A) => b); } } const uppercase = new ReaderMonad((s: string) => s.toUpperCase()) const reverse = new ReaderMonad((s: string) => s.split('').reverse().join('')); const reader = uppercase .bind(s1 => reverse .bind(s2 => ReaderMonad.return([s1, s2]))); const result = reader.runReader("hello"); //["HELLO", "olleh"] -
venil7 created this gist
Apr 6, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ class ReaderMonad<A,B> { constructor(private runReader: (a:A) => B) { } bind<C>(func: (b:B) => ReaderMonad<A,C>): ReaderMonad<A,C> { return new ReaderMonad<A, C>((a:A) => { const b = this.runReader(a); return func(b).runReader(a); }); } static return<A, B>(b: B): ReaderMonad<A,B> { return new ReaderMonad((a: A) => b); } }