score:80
It makes you able to do e.g.:
scala> def foo(as: Int*)(bs: Int*)(cs: Int*) = as.sum * bs.sum * cs.sum
foo: (as: Int*)(bs: Int*)(cs: Int*)Int
scala> foo(1, 2, 3)(4, 5, 6, 7, 9)(10, 11)
res7: Int = 3906
score:15
I know one of the motivations was implicit parameter lists. "implicit" is a property of the list, not the parameter. Another was probably case classes: only the first parameter list become case fields.
score:22
Back references in default arguments:
case class Foo(bar: Int)
def test(f: Foo)(i: Int = f.bar) = i*i
test(Foo(3))()
score:30
To answer your "related question," currying is simply a way of turning a function of multiple arguments, for example (A, B, C) => D
, into a function which takes one argument and returns a function, e.g. A => (B => (C => D))
(parentheses shown but not necessary).
The tuple-ized form and the curried form are isomorphic, and we may translate freely between them. All of these are equivalent, but have different syntactic implications:
(A, B, C, D, E) => F
((A, B), (C, D, E)) => F
(A, B) => (C, D, E) => F
When you declare separate parameter groups, this is the kind of currying you're doing. The multi-parameter-group method is a method which returns a function... you can see this in the REPL:
scala> def foo(a:Int, b:Int)(c:Int, d:Int, e:Int):Int = 9
foo: (a: Int,b: Int)(c: Int,d: Int,e: Int)Int
scala> foo _
res4: (Int, Int) => (Int, Int, Int) => Int = <function2>
score:49
As well as allowing you to write methods that look like part of the language (which you already spotted), it's worth noting that the type inferencer will work with one block at a time.
So in this:
def foo[T](a: T, b: T)(op: (T,T)=>T) = op(a,b)
foo(1,2){_+_}
T
will first be inferred as Int
, which will then be used as the type of the two underscores in the closure.
This is how the compiler then knows, with complete type safety, that the + operation is valid.
Source: stackoverflow.com
Related Query
- Why does Scala provide both multiple parameters lists and multiple parameters per list?
- What's the difference between multiple parameters lists and multiple parameters per list in Scala?
- Scala by name parameters with multiple argument lists and currying
- Why does Scala choose the type 'Product' for 'for' expressions involving Either and value definitions
- Scala Actors: if react never returns, why does it need to be in a loop{}, and why doesn't while(true) work?
- why Scala does not have concept of checked and unchecked exception?
- Why does Scala evaluate the argument for a call-by-name parameter if the method is infix and right-associative?
- Why does currying in Scala need multiple parameter lists?
- Quasiquotes for multiple parameters and parameter lists
- Scala and SLF4J :: pass multiple parameters
- When does it make sense to use implicit parameters in Scala, and what may be alternative scala idioms to consider?
- Scala Option? Why does Some not inherit AnyVal and why is it not a value type?
- Modifying multiple Lists inside a function and returning it in Scala
- Why does this code typecheck in Scala 2.11 and what can I do about it?
- Why does Scala define a "+=" operator for Short and Byte types?
- Why does sbt give multiple dependencies warning with Akka and ScalaTest dependencies?
- Why does scala reduce() and reduceLeft() reduce the sequence of the values in the same way?
- Multiple parameters lists and default arguments
- Why does Scala separate symbols and letters in identifiers, and can I change this?
- Why does this snippet with pattern matching and higher kinded types no longer compile in Scala 2.12?
- Scala type inference for both a generic type and it's type parameter - why doesn't it work?
- Why does scala compiler fail to find implicit parameter value/conversion when it is an overload and has generic type param?
- Why does scala map java.lang.Object parameters of java-defined methods to scala.Any?
- Why does the same algorithm work in Scala much slower than in C#? And how to make it faster?
- Why does Scala not infer the type parameters when pattern matching with @
- Why does scala evaluate a type alias when it has a line break and no equals sign?
- Why is no implicit view found, when eta conversion and specifying type parameters does allow an implicit view to be found?
- Why does SBT 0.12.2 resolve plugins with Scala 2.9.2 and ignore scalaVersion in build.sbt?
- How does promise linking work in Scala and why is it necessary?
- Why does '0xcafebabe' stand for a different number in Scala and Python?
More Query from same tag
- Convert seconds to hhmmss Spark
- Issue with companion objects in scala
- Converting JsValue to String
- Apache spark taking average by key with python
- Stackoverflow exception in a PersistentFSM actor
- Why sbt does override scala version in subproject?
- Scala Spark IntelliJ Idea development process
- Package method having no effect in Buildr
- Reading a .sql file from local Resources when Running my job on Cluster
- How to efficiently query a hive table in spark using hive context?
- GridEngine qstat no output?
- SQL `NULL` read at column 1 (JDBC type null) but mapping is to a non-Option type
- Counterintuitive suggestion in the "missing argument list"-error message for methods with multiple argument lists?
- How do i read and write to the same file in scala?
- how to move files located in on-premises windows based file share using scala?
- Have && short circuit with :| in expressions in ScalaCheck
- How to pass hiveContext as argument to functions spark scala
- Calling type classes by using the type from a variable
- Elegant Json flatten in Spark
- How would I write a method to turn a binary search tree (BST) into a sorted list for the values in the BST?
- automatic xml conversion in scala
- Scala type bounds versus Foo[_]
- What are the differences between Akka and Netty besides their choice of language (Scala vs Java)?
- Comparing Lift with Play2
- NoClassDefFoundError: Could Not Initialise class org.apache.spark.package
- Scala inner types as abstract method arguments
- Scala check if string in option is defined and empty
- Why Scala changed relative precedence of relational vs equality operators (compared to Java)?
- Yield to foreach - refactor
- Implementing common functionality across different TableQuery[T]