Created
August 10, 2018 17:18
-
-
Save mvaldesdeleon/edb0dd5adaa04d40cd385ee48f8b012f 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 MonoidCtor<A> { | |
| new (a: any): Monoid<A>; | |
| of (value: any): Monoid<A>; | |
| empty (): Monoid<A>; | |
| concat: (a: Monoid<A>) => (b: Monoid<A>) => Monoid<A>; | |
| } | |
| interface Monoid<A> { | |
| empty: () => Monoid<A>; | |
| concat: (a: Monoid<A>) => Monoid<A>; | |
| } | |
| class Sum implements Monoid<Sum> { | |
| public static of(value: number) { | |
| return new Sum(value); | |
| } | |
| public static empty() { | |
| return new Sum(0); | |
| } | |
| public static concat = (a: Sum) => (b: Sum) => Sum.of(a.value + b.value); | |
| constructor(private value: number) {} | |
| empty() { | |
| return Sum.empty(); | |
| } | |
| concat(a: Sum) { | |
| return Sum.concat (this) (a); | |
| } | |
| getSum() { | |
| return this.value; | |
| } | |
| } | |
| class Prod implements Monoid<Prod> { | |
| public static of(value: number) { | |
| return new Prod(value); | |
| } | |
| public static empty() { | |
| return new Prod(1); | |
| } | |
| public static concat = (a: Prod) => (b: Prod) => Prod.of(a.value * b.value); | |
| constructor(private value: number) {} | |
| empty() { | |
| return Prod.empty(); | |
| } | |
| concat(a: Prod) { | |
| return Prod.concat (this) (a); | |
| } | |
| getProd() { | |
| return this.value; | |
| } | |
| } | |
| let s = Sum.of(1); | |
| let t = Sum.of(2); | |
| console.log(Sum.concat(s)(t).getSum()); | |
| let p = Prod.of(1); | |
| let q = Prod.of(2); | |
| console.log(Prod.concat(p)(q).getProd()); | |
| console.log( [1,2,3,4].reduce ((p, v) => p.concat(Prod.of(v)), Prod.empty()) ); | |
| console.log( [1,2,3,4].reduce ((p, v) => p.concat(Sum.of(v)), Sum.empty()) ); | |
| const foldMap = <A>(typeRep: MonoidCtor<A>, xs: any[]): A => xs.reduce((m, x) => m.concat(typeRep.of(x)), typeRep.empty()); | |
| const concat = <A>(a: Monoid<A>, b: Monoid<A>): Monoid<A> => a.concat(b); | |
| let a = foldMap<Sum>(Sum, [1,2,3,4]); | |
| let b = foldMap<Prod>(Prod, [1,2,3,4]); | |
| let c = concat<Sum>(s, t); | |
| let d = concat<Prod>(p, q); | |
| let e = concat<Sum>(s, q); // no error here :( | |
| console.log(a.getSum()); | |
| console.log(a.getProd()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment