Skip to content

Instantly share code, notes, and snippets.

@mvaldesdeleon
Created August 10, 2018 15:58
Show Gist options
  • Save mvaldesdeleon/cbdd394d915bd351d24a7d46b90da9fe to your computer and use it in GitHub Desktop.
Save mvaldesdeleon/cbdd394d915bd351d24a7d46b90da9fe to your computer and use it in GitHub Desktop.
interface Monoid<A, B> {
mempty: () => Monoid<A, B>;
mconcat: (r: Monoid<A, B>) => Monoid<A, B>;
runMonoid: () => B;
}
const mconcat = <A, B>(a : Monoid<A, B>) => (b: Monoid<A, B>): Monoid<A, B> => a.mconcat(b);
class Sum implements Monoid<Sum, number> {
constructor(private value: number) {}
mempty() {
return new Sum(0);
}
mconcat(sum: Sum) {
return new Sum(this.value + sum.value);
}
runMonoid() {
return this.value;
}
}
class Prod implements Monoid<Prod, number> {
constructor(private value: number) {}
mempty() {
return new Prod(1);
}
mconcat(prod: Prod) {
return new Prod(this.value * prod.value);
}
runMonoid() {
return this.value;
}
}
let s = new Sum(1);
let t = new Sum(2);
let u = new Prod(2);
let v = new Prod(3);
let a = mconcat (s) (t.mempty());
let b = mconcat<Prod, number> (u) (s);
console.log(a.runMonoid());
console.log(b.runMonoid());
// interface Functor<F> {
// map<A, B>(f: (a: A) => B): F<A> => T<B>;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment