3 ms·
Is currying useful other than as a shorthand for "value object" creation? You can easily create the equivalent of a curried add function: class Adder {
by h2j24 9y ago
Is currying useful other than as a shorthand for "value object" creation? You can easily create the equivalent of a curried add function:
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.
- curun1r 9y agoEasier to compose with other objects, but harder to compose with other functions. For example: const compose = (...f) => f.reduceRight((f, g) => (...x) => f(g(...x))); const add = x => y => y + x; const mul = x => y => y * x; const div = x => y => y / x; // (6x + 4) / 2 const complexOp = compose(mul(6), add(4), div(2)); // should return 50 complexOp(16); How would you accomplish this using classes?
- h2j24 9y agoclass 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.