Skip to content

Instantly share code, notes, and snippets.

@maciejsikora
Created May 16, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maciejsikora/acc1b0093def85b54fec44accb9c6e49 to your computer and use it in GitHub Desktop.
Save maciejsikora/acc1b0093def85b54fec44accb9c6e49 to your computer and use it in GitHub Desktop.
Maybe Functor
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