Scala currying vs partially applied functions

后端 未结 5 691
旧巷少年郎
旧巷少年郎 2020-11-27 09:57

I realize that there are several questions on here about what currying and partially applied functions are, but I\'m asking about how they are diff

5条回答
  •  感动是毒
    2020-11-27 10:15

    Currying is to do with tuples: turning a function that takes a tuple argument into one that takes n separate arguments, and vice versa. Remembering this is the key to distinguishing curry vs partial application, even in languages that don't cleanly support currying.

    curry :: ((a, b) -> c) -> a -> b -> c 
       -- curry converts a function that takes all args in a tuple
       -- into one that takes separate arguments
    
    uncurry :: (a -> b -> c) -> (a, b) -> c
       -- uncurry converts a function of separate args into a function on pairs.
    

    Partial application is the ability to apply a function to some arguments, yielding a new function for the remaining arguments.

    It is easy to remember if you just think currying is the transformation to do with tuples.

    In languages that are curried by default (such as Haskell) the difference is clear -- you have to actually do something to pass arguments in a tuple. But most other languages, including Scala, are uncurried by default -- all args are passed as tuples, so curry/uncurry is far less useful, and less obvious. And people even end up thinking that partial application and currying are the same thing -- just because they can't represent curried functions easily!

提交回复
热议问题