2 ms·
class Answer { getAnswer(input: number) { return (input * 6 + 4) / 2; } } All joking aside, you would achieve composition the same way, except usin
by h2j24 9y ago
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.