klmr / fun

Module for functional programming in R
17 stars 0 forks source link

Add `curry` and `uncurry` #9

Open klmr opened 7 years ago

klmr commented 7 years ago

Add curry and uncurry functions. I’m not sure they’re particularly useful to be honest but given the confusion between currying and partial application in the R community it would have a nice educational effect to contrast the two operations of currying and partial function application.

Semantics (WLOG):

  1. curry(f) returns a function that’s a curried version of f.
  2. uncurry(g) returns a function that’s an uncurried version of g.
  3. length(args(curry(f))) == 1.
  4. identical(uncurry(curry(f)), f).

Point (4) in particular might require some gymnastics if f is a closure — although strict identity is probably not required as long as the observable effect is identical.

klmr commented 7 years ago

Something to think about: currying a function with an ellipsis presumably yields an infinitely recursive function. Not clear how to resolve this, maybe just forbid it.