Last active
January 4, 2018 18:11
-
-
Save jhusain/e9ca043a6fb419683b7a8aebfb00801d to your computer and use it in GitHub Desktop.
You can't write fold in TypeScript (doesn't compile)
This file contains 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 Combinable<T> { | |
plus(T): T, | |
zero(): T | |
} | |
class Max implements Combinable<Max> { | |
private value: number | |
constructor(num: number) { | |
this.value = num; | |
} | |
plus(m) { | |
if (this.value > m.value) | |
return this; | |
else | |
return m; | |
} | |
zero() { | |
return new Max(Number.MIN_VALUE); | |
} | |
} | |
function fold<T extends Combinable<T>>(array: T[], fn: (left: T, right: T) => T) : T { | |
return reduce((left, right) => left.plus(right)) T.zero() array | |
} | |
function add(x: number, f: (left :number) => number): number { | |
return f(x); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment