Tutorial by Examples: currying

def numberOrCharacterSwitch(toggleNumber: Boolean)(number: Int)(character: Char): String = if (toggleNumber) number.toString else character.toString // need to explicitly specify the type of the parameter to be curried // resulting function signature Boolean => String val switchBetween3A...
def minus(left: Int, right: Int) = left - right val numberMinus5 = minus(_: Int, 5) val fiveMinusNumber = minus(5, _: Int) numberMinus5(7) // 2 fiveMinusNumber(7) // -2
Let's define a function of 2 arguments: def add: (Int, Int) => Int = (x,y) => x + y val three = add(1,2) Currying add transforms it into a function that takes one Int and returns a function (from one Int to an Int) val addCurried: (Int) => (Int => Int) = add2.curried // ...
Currying, according to Wikipedia, is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions. Concretely, in terms of scala types, in the context of a function that take two arguments, (has arity 2) it is the conversion o...
Currying is the transformation of a function of n arity or arguments into a sequence of n functions taking only one argument. Use cases: When the values of some arguments are available before others, you can use currying to decompose a function into a series of functions that complete the work in s...
Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. This is normally useful when for example: different arguments of a function are calculated at different times. (Example 1) di...
def pow = { base, exponent -> base ** exponent } assert pow(3, 2) == 9 def pow2 = pow.curry(2) //base == 2 assert pow2(3) == 8
def dividable = { a, b -> a % b == 0 } assert dividable(2, 3) == false assert dividable(4, 2) == true def even = dividable.rcurry(2) // b == 2 assert even(2) == true assert even(3) == false
def quatNorm = { a, b, c, d -> Math.sqrt(a*a + b*b + c*c + d*d) } assert quatNorm(1, 4, 4, -4) == 7.0 def complexNorm = quatNorm.ncurry(1, 0, 0) // b, c == 0 assert complexNorm(3, 4) == 5.0
def noParam = { "I have $it" } def noParamCurry = noParam.curry(2) assert noParamCurry() == 'I have 2'
def honestlyNoParam = { -> "I Don't have it" } // The following all throw IllegalArgumentException honestlyNoParam.curry('whatever') honestlyNoParam.rcurry('whatever') honestlyNoParam.ncurry(0, 'whatever')
In Haskell, all functions are considered curried: that is, all functions in Haskell take just one argument. Let's take the function div: div :: Int -> Int -> Int If we call this function with 6 and 2 we unsurprisingly get 3: Prelude> div 6 2 3 However, this doesn't quite behave in...
What we have is a list of credit cards and we'd like to calculate the premiums for all those cards that the credit card company has to pay out. The premiums themselves depend on the total number of credit cards, so that the company adjust them accordingly. We already have a function that calculate...
Use the uncurry function (from Prelude or Data.Tuple) to convert a binary function to a function on tuples. uncurry (+) (1, 2) -- computes 3 uncurry map (negate, [1, 2, 3]) -- computes [-1, -2, -3] uncurry uncurry ((+), (1, 2)) -- computes 3 map (uncurry (+)) [(1, 2), (3, 4), (5, 6)] -- co...
Use the curry function (from Prelude or Data.Tuple) to convert a function that takes tuples to a function that takes two arguments. curry fst 1 2 -- computes 1 curry snd 1 2 -- computes 2 curry (uncurry f) -- computes the same as f import Data.Tuple (swap) curry swap 1 2 -- computes (2, 1...
One application of closures is to partially apply a function; that is, provide some arguments now and create a function that takes the remaining arguments. Currying is a specific form of partial application. Let's start with the simple function curry(f, x) that will provide the first argument to a ...
Technically, Ruby doesn't have functions, but methods. However, a Ruby method behaves almost identically to functions in other language: def double(n) n * 2 end This normal method/function takes a parameter n, doubles it and returns the value. Now let's define a higher order function (or met...
Currying is the process of transforming a function that takes multiple arguments into a sequence of functions that each has only a single parameter. Currying is related to, but not the same as, partial application. Let's consider the following function in JavaScript: var add = (x, y) => x + y ...

Page 1 of 1