Skip to content

Instantly share code, notes, and snippets.

@piq9117
Forked from co1rowjp/gist:3845888
Last active May 8, 2017 03:07
Show Gist options
  • Save piq9117/92f98df7b1d20bd06d921607f9b120dd to your computer and use it in GitHub Desktop.
Save piq9117/92f98df7b1d20bd06d921607f9b120dd to your computer and use it in GitHub Desktop.
TypeScript Maybe
interface Functor {
fmap: (any) => any;
}
interface Monad extends Functor {
bind: (any) => Monad;
}
interface Maybe extends Monad {
}
class Just implements Maybe {
private value: any;
constructor(a: any) {
this.value = a
}
fmap (f: (any) => any): Just {
return new Just(f(this.value))
}
bind (f: (any) => Maybe): Maybe {
return f(this.value)
}
}
class Nothing implements Maybe {
fmap (f: (any) => any): Nothing {
return this
}
bind (f: (any) => Maybe): Maybe {
return this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment