1

What is the difference between the following two functions definition? Please bear with me if this has been answered before. I don't know what is the first type of function called to search for properly.

def f1(a: String, b: T)(c: String, d: String, e: String = "_id"): Unit = ???

and

def f1(a: String, b: T, c: String, d: String, e: String = "_id"): Unit = ???

I know that it is similar to the question mentioned here.

  • Q1: What is the difference ?
  • Q2: What are the advantages of defining currying functions ?
Community
  • 1
  • 1
eliasah
  • 39,588
  • 11
  • 124
  • 154
  • 2
    I'm on mobile and can't write an actual answer, but you can read about partially applied functions and currying: http://stackoverflow.com/questions/14309501/scala-currying-vs-partially-applied-functions – Andy Ibanez Jan 06 '16 at 08:41
  • Thanks, I'll read it and if there is some specifics that I don't get, I'll update my question or add a comment here. – eliasah Jan 06 '16 at 08:43
  • Ok, just before closing the question, just so I can be sure of something, this first one is called a currying function? – eliasah Jan 06 '16 at 08:55
  • 1
    to be precise it's a curried function. Currying is a process of changing a multi parameter function into a chain of functions – Mateusz Dymczyk Jan 06 '16 at 09:01
  • Neither of the two are function definitions. Both are method definitions. – Jörg W Mittag Jan 06 '16 at 11:04
  • Ok this is the source of my confusion, I can't figure out what is the name of what anymore thanks to Google... – eliasah Jan 06 '16 at 11:05
  • @eliasah Currying also a benefits from type inference: http://stackoverflow.com/q/32030455/1560062 – zero323 Feb 08 '16 at 15:38
  • @zero323 Thanks ! ;) – eliasah Feb 08 '16 at 15:47

1 Answers1

3

A1:

  • The difference is that in the latter you have to provide all the parameters at the same time.

A2:


EDIT Advantages / Disadvantages concept-wise

lambda calculus semplicity:

In theoretical computer science, currying provides a way to study functions with multiple arguments in very simple theoretical models such as the lambda calculus in which functions only take a single argument.

(nice example here: https://en.wikipedia.org/wiki/Currying#Motivation)

Curried functions needs closure support:

Curried functions may be used in any language that supports closures

Uncurried functions for performance:

Uncurried functions are generally preferred for efficiency reasons, since the overhead of partial application and closure creation can then be avoided for most function calls.

Curry–Howard correspondence:

The existence of currying and uncurrying is equivalent to the logical theorem enter image description here as tuples (product type) corresponds to conjunction in logic, and function type corresponds to implication.

Source: https://en.wikipedia.org/wiki/Currying

Filippo Vitale
  • 7,597
  • 3
  • 58
  • 64