The errors package is really useful for generating stack traces in Go 1: https://godoc.org/github.com/pkg/errors
class Answer {
getAnswer(input: number) { return (input * 6 + 4) / 2; }
}
All joking aside, you would achieve composition the same way, except using interfaces. interface Operation<T> {
apply(input: T);
}
class Addition implements Operation<Number> {
}
class Multiplication implements Operation<Number> {
}
class Division implements Operation<Number> {
}
function compose(Operation<T>... operation, T input) {
T last = input;
for (Operation<T> op : operations) {
last = op.apply(last);
}
return last;
}
Of course, the above code is slightly verbose, but even if you could remove all the boiler plate it still doesn't look like good imperative code. This is what confuses me about currying: when translated to the imperative equivalent, it looks terrible. class Adder {
base: number;
constructor(base: number) { this.base = base; }
add(o: number): number { return this.base + o; }
}
While slightly more verbose, I believe this form to be the more powerful of the two because one has access to all of the features of imperative programming and is easier to compose with other objects.