Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, currying refers to the technique of transforming a function that takes multiple arguments into a series of functions that each take a single argument. It's a way to break down a function that takes multiple arguments into a chain of functions that each take one argument.
Here's a simple example to illustrate currying in Scala:
def add(x: Int, y: Int): Int = x + y
Usage:
val result = add(3, 4) // result is 7
def addCurried(x: Int)(y: Int): Int = x + y
Usage:
val result = addCurried(3)(4) // result is 7
The curried function addCurried
can also be used in a partially applied form:
val addThree = addCurried(3)_ val result = addThree(4) // result is 7
Here, addThree
is a function that takes one integer and adds 3 to it.
If you have a function and want to convert it to its curried form, you can use the curried
method:
val addFunction = (add _).curried val addFive = addFunction(5) val result = addFive(6) // result is 11
You can extend currying to functions with more than two parameters:
def multiplyCurried(a: Int)(b: Int)(c: Int) = a * b * c
Usage:
val partial1 = multiplyCurried(2)_ val partial2 = partial1(3)_ val result = partial2(4) // result is 24 (2 * 3 * 4)
Currying is beneficial when you want to specialize functions, create more reusable pieces of functionality, or prepare for higher-order function operations.
Scala currying examples:
def add(x: Int)(y: Int): Int = x + y val curriedAdd: Int => Int = add(5) val result: Int = curriedAdd(3) // Result: 8
Introduction to currying in Scala:
def multiply(x: Int)(y: Int): Int = x * y val curriedMultiply: Int => Int = multiply(3) val result: Int = curriedMultiply(4) // Result: 12
Partial application and currying in Scala:
def power(base: Double, exponent: Double): Double = math.pow(base, exponent) val square: Double => Double = power(_: Double, 2) val result: Double = square(4) // Result: 16.0
Currying vs. partial application in Scala:
def add(x: Int, y: Int): Int = x + y val curriedAdd: Int => Int => Int = (add _).curried val partiallyAppliedAdd: Int => Int = add(5, _)
Benefits of currying in functional programming with Scala:
def curryExample(x: Int)(y: Int)(z: Int): Int = x + y * z val partiallyApplied: Int => Int = curryExample(5)(_)
Currying and higher-order functions in Scala:
def operation(f: Int => Int)(x: Int): Int = f(x) val square: Int => Int = x => x * x val result: Int = operation(square)(3) // Result: 9
Scala curry and uncurry operations:
curry
and uncurry
operations in Scala transform between a curried function and a function with multiple parameters.def curry[A, B, C](f: (A, B) => C): A => B => C = a => b => f(a, b) def uncurry[A, B, C](f: A => B => C): (A, B) => C = (a, b) => f(a)(b)
Curried functions and type inference in Scala:
def curriedAdd(x: Int)(y: Int): Int = x + y val result: Int = curriedAdd(3)(4) // Result: 7
Currying in Scala standard library functions:
val addOne: Int => Int = (1).+ val result: Int = addOne(5) // Result: 6
Advanced currying techniques in Scala:
def advancedCurrying[A, B, C](f: A => B => C): A => (B => C) = a => b => f(a)(b)
Currying in Scala and function composition:
def compose[A, B, C](f: B => C, g: A => B): A => C = a => f(g(a))