Created
May 16, 2019 09:07
-
-
Save maciejsikora/acc1b0093def85b54fec44accb9c6e49 to your computer and use it in GitHub Desktop.
Maybe Functor
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
class Just<Data> { | |
value: Data; | |
constructor(value: Data) { | |
this.value = value; | |
} | |
fmap<NextData>(f: (x: Data) => NextData) { | |
return new Just<NextData>(f(this.value)) | |
} | |
} | |
class Nothing { | |
fmap(f: (x: any) => any) { | |
return new Nothing(); | |
} | |
} | |
type Maybe<Data> = Just<Data> | Nothing; | |
const something = new Just(1); something.fmap(x => x + 1).fmap(x => x + 2); // is doing comutation | |
const nothing = new Nothing(); nothing.fmap(x => x + 1).fmap(x => x + 2); // id doing nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment