Created
August 10, 2018 15:58
-
-
Save mvaldesdeleon/cbdd394d915bd351d24a7d46b90da9fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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