Skip to content

Instantly share code, notes, and snippets.

@jhusain
Last active January 4, 2018 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhusain/e9ca043a6fb419683b7a8aebfb00801d to your computer and use it in GitHub Desktop.
Save jhusain/e9ca043a6fb419683b7a8aebfb00801d to your computer and use it in GitHub Desktop.
You can't write fold in TypeScript (doesn't compile)
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