Ordering of parameters to make use of currying

For languages that support currying and partial-application easily, there is one compelling series of arguments, originally from Chris Okasaki: Put the data structure as the last argument Why? You can then compose operations on the data nicely. E.g. insert 1 $ insert 2 $ insert 3 $ s. This also helps for functions on state. … Read more

Does Java support Currying?

Java 8 (released March 18th 2014) does support currying. The example Java code posted in the answer by missingfaktor can be rewritten as: import java.util.function.*; import static java.lang.System.out; // Tested with JDK 1.8.0-ea-b75 public class CurryingAndPartialFunctionApplication { public static void main(String[] args) { IntBinaryOperator simpleAdd = (a, b) -> a + b; IntFunction<IntUnaryOperator> curriedAdd = … Read more

How do I write a function that returns another function?

Try this, using Python: import math def make_cylinder_volume_func(r): def volume(h): return math.pi * r * r * h return volume Use it like this, for example with radius=10 and height=5: volume_radius_10 = make_cylinder_volume_func(10) volume_radius_10(5) => 1570.7963267948967 Notice that returning a function was a simple matter of defining a new function inside the function, and returning … Read more

What does lambda with 2 arrows mean in Java 8?

If you express this as non-shorthand lambda syntax or pre-lambda Java anonymous class syntax it is clearer what is happening… The original question. Why are two arrows? Simple, there are two functions being defined… The first function is a function-defining-function, the second is the result of that function, which also happens to be function. Each … Read more

JavaScript curry: what are the practical applications?

Here’s an interesting AND practical use of currying in JavaScript that uses closures: function converter(toUnit, factor, offset, input) { offset = offset || 0; return [((offset + input) * factor).toFixed(2), toUnit].join(” “); } var milesToKm = converter.curry(‘km’, 1.60936, undefined); var poundsToKg = converter.curry(‘kg’, 0.45460, undefined); var farenheitToCelsius = converter.curry(‘degrees C’, 0.5556, -32); milesToKm(10); // returns … Read more

How does functools partial do what it does?

Roughly, partial does something like this (apart from keyword args support etc): def partial(func, *part_args): def wrapper(*extra_args): args = list(part_args) args.extend(extra_args) return func(*args) return wrapper So, by calling partial(sum2, 4) you create a new function (a callable, to be precise) that behaves like sum2, but has one positional argument less. That missing argument is always … Read more

What is the difference between currying and partial application?

Currying is converting a single function of n arguments into n functions with a single argument each. Given the following function: function f(x,y,z) { z(x(y));} When curried, becomes: function f(x) { lambda(y) { lambda(z) { z(x(y)); } } } In order to get the full application of f(x,y,z), you need to do this: f(x)(y)(z); Many … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)