Hacker News new | ask | show | jobs
by Jtsummers 1358 days ago
Not quite, that is partial application. Actual currying would allow you to do partial application this way, making it less explicit:

  > g = f(3) # no second parameter provided
  g = <unary function> # however it would be indicated in the python shell
To get it in Python you'd need to do this:

  f = lambda x: lambda y: x + y
  g = f(3) # result is a function, or funcallable, or whatever Python calls the resultant type
This makes the currying explicit. In general, currying is taking an n-ary function and reducing it to a series of n unary functions. These three are equivalent, in Haskell(ish, rusty):

  f x y = x + y
  g x = \y -> x + y
  h = \x -> \y -> x + y
In Haskell, you don't have to use the explicit form of `h` (or the explicit form in the Python example) if you don't want to (and it would probably be weird if you used it extensively).
1 comments

> Actual currying would allow you to do partial application this way, making it less explicit

> To get it in Python you'd need to do this:

> f = lambda x: lambda y: x + y

> This makes the currying explicit. In general, currying is taking an n-ary function and reducing it to a series of n unary functions.

Something's gone wrong; your example of explicit currying conflicts with your definition of currying. `f` is a unary function.

The original `f` was a binary function:

  f = lambda x, y: x + y
My updated curried `f` is a unary function, but it returns a unary function itself:

  f = lambda x: lambda y: x + y
That's what currying is. I just reused the name, if you prefer:

  curried_f = lambda x: lambda y: x + y
EDIT:

And note, it makes the partial application less explicit/verbose than the original, but the currying is very explicit because of Python's syntax.

  g = curried_f(3)
Is "just" a function call, while:

  g = lambda y: f(3, x)
does the same thing (functionally, at least) but the partial application is made explicit.