How currying works - scala

I have following function with parameter lists:
def foo(a:Int, b:Int)(x:Int)(y:Int) = a * b + x - y
when I put the command on the relp
foo _
it shows me
res5: (Int, Int) => Int => (Int => Int) = <function2>
The first part expects two int parameters and then I do not know anymore how to read continue.
I can use the function like
foo(5,5)(10)(10)
but I do not know how to read it!

In general, A => B is the type of an anonymous function that, when given an argument of
type A, returns a value of type B.
Now, the type
(Int, Int) => Int => (Int => Int)
might look less confusing when you add some parenthesis:
(Int, Int) => (Int => (Int => Int))
As a start, just break it down to
(Int, Int) => (… => …)
i.e. a function that, given two Int, returns another function. The
function that is returned has type
(Int => (Int => Int))
i.e. a function that, given an Int, returns another function that, given an Int,
returns an Int.

It's easy. (Int, Int) => Int => (Int => Int) means that 2 first steps returns a function that will take some arguments. For example:
(Int, Int) => Function that return
(Int) => Function that return
(Int) => Int
Why is it useful? Because when you have a function that take a function as parameter in of it's arguments list you can omit parentheses and use curly braces.
def someFunStuff(fun: Int)(stuff: Int => Int) = {
fun*stuff(fun)
}
someFunStuff(2) { x =>
//Do some fun stuff
x * 2 / x
}
If you want to know a little bit more, please read Chapter 9.4 programming in Scala second edition.

Related

Scala Curry-Uncurry A Function

I am trying to create a function that receives a basic curried adder function with 2 parameters and returns uncurried adder function, and vice versa for the currying function(receives uncurried-returns curried) in scala. I am having a hard time figuring the return types of curried functions , can anyone help out?
def adderCurried(a: Int)(b: Int): Int = a + b
//define a function that returns uncurried version of this function:
val adderUncurried = toAdderUncurried(adderCurried)
adderUncurried(5,6) // returns 11
def adder(a: Int, b: Int): Int = a + b
//define a function that returns curried version of this function:
val adderCurried = toAdderCurried(adder)
adderCurried(5,6) // returns 11
Part of the problem is that you are not calling the curried function correctly:
adderCurried(5,6) // too many arguments (found 2, expected 1)
It should be called in a way that matches the declaration:
def adderCurried(a: Int)(b: Int)
adderCurried(5)(6)
This gives the clue that there are actually two function invocations here. The first invocation (5) returns a function that when called with (6) gives the answer. That second function must take an Int and return an Int so it is Int => Int, and this must be what is returned by adderCurried(5).
So adderCurried takes an Int and returns Int => Int so the type is
Int => (Int => Int)
or just
Int => Int => Int
You can check this as follows:
val check: Int => Int => Int = adderCurried // OK
You should be able to create the signatures for toAdderCurried and toAdderUncurried based on these types.
The toAdderUncurried function will be like this.
def toAdderUncurried(f: Int => Int => Int): (Int, Int) => Int = (x, y) => f(x)(y)
You can call it like this.
toAdderUncurried(adderCurried)(x, y)
And the curry function will be like this.
def curry(f: (Int, Int) => Int): Int => Int => Int = x => y => f(x, y)
you can call it like this
curry(toAdderUncurried(adderCurried))(x)(y)
Let me add Tim answer. Right here you have 2 options of your adder function:
curried type Curried = (Int => Int => Int)
and uncurried type Uncurried = (Int, Int) => Int
and your signatures of converting functions should looks like:
def toAdderUncurried(adderCurried: Curried): Uncurried = ???
def toAdderCurried(adderUncurried: Uncurried): Curried = ???

Scala lambda function not resolved when declares the type of parameter?

when defining method in Scala, I found this
def method1: Int => Int = (j: Int) => j // works
def method2: Int => Int = j => j // works
def method3: Int => Int = j: Int => j // error
def method4: Int => Int = {j: Int => j} // works
Can anyone explain why method3 does not work? Is there any ambiguity in it?
One possible explanation is indeed that this restriction avoids ambiguity: x: A => B could be understood as an anonymous function that takes a parameter x of type A and returns the object B. Or it could be understood as "casting" a variable x to the type A => B. It is very rare that both of these would be valid programs, but not impossible. Consider:
class Foo(val n: Int)
val Foo = new Foo(0)
val j: Int => Foo = new Foo(_)
def method1: Int => Foo = (j: Int) => Foo
def method2: Int => Foo = j: Int => Foo
println(method1(1).n)
println(method2(1).n)
This actually compiles and prints:
0
1
All of these variants are covered by Anonymous Functions section of the specification. The relevant part is
In the case of a single untyped formal parameter, (x) => e
can be abbreviated to
x => e. If an anonymous function (x : T) => e
with a single typed parameter appears as the result expression of a block, it can be abbreviated to
x: T => e.
In method3, the function isn't the result expression of a block; in method4 it is.
EDIT: oops, presumably you meant why the limitation is there. I'll leave this answer for now as stating what the limitation is exactly, and remove it if anyone gives a better answer.

Invoking higher order functions - Scala

Hi I am new to Scala and trying to call a higher order function sum_of from main class.I am getting "Cannot resolve reference sumOf with such signature error".
object SumOf {
def main(args: Array[String]) {
val y = sumOf(x=>x ,4,5)
println(y)
}
def sumOf(f: Int => Int)(a: Int, b: Int): Int = {
def loop(a: Int, acc: Int): Int =
if (a > b) acc
else loop(a + 1, f(a) + acc)
loop(a, 0)
}
}
sumOf is a curried function so it takes two arguments in the form of sumOf(x => x)(4,5) which is different from sumOf(x => x, 4,5). This is the reason you are getting an error.
Further, you can call it with only one argument sumOf(x => x) _ which returns another function that takes the second argument
(Int, Int) => Int = <function2> and return a function. This is more commonly known as partial function application.
Your sumOf method has two argument lists, and needs to be called with two argument lists.
val y = sumOf(x => x)(4, 5)
You can think of sumOf as a function which takes an Int => Int and returns a new function, which takes two Ints to return an Int.

Unsure how this returns an Int of functions

I'm reading this scala book and a bit unsure on what this syntax translates to:
def bf: Int => Int => Int = i => v => i + v
bf is the name of the function.
after then colon is the function return type which is:
Int => Int => Int
I presume that the equal sign is the start of the function body, but not sure because then it has i => v => i + v.
Can someone clarify where each return type is and how to you break it down in your head so I can do the same :)
Short answer:
return type of the code can be read as Int => (Int => Int).
It is a function that takes single parameter of Int and returns a function that takes another Int parameter and returns Int.
If you are still getting confused, this is the longer way to write the same.
val bf: Int => (Int => Int) = (a:Int) => ((b: Int) => a + b)
|_________________| |____________________________|
type outer function
|________________|
inner func returned by the outer func
Long answer:
It is normally called curried function or function currying.
I assume You already noticed this though, This method originally trying to get sum of 2 Int values which is written as below.
val foo = (a: Int,b: Int) => a + b
any functions which takes multiple parameters can be transformed to nested single parameter functions a.k.a "curried function".
You may write it by your self just like your question code but You can call .curried to the method/function to get it as well.
val bf = foo.curried //bf is exactly the same thing as you wrote
This is usually used when you can't define both of the parameters at the same time so when you gets first value, make a function that (b: Int) => YOUR_FIRST_VALUE_ALREADY_DEFINED + b. and call it when you get second value.
How the curried function is used...
bf(4)(5) //9
val bar = bf(6) // (Int) => Int
bar(7) // 13
functional types are associate to the right.
for e.g.
Int => Int => Int is equivalent to Int => (Int => Int)
more details you can register and go through below lecture.
https://www.coursera.org/learn/progfun1/lecture/fOuQ9/lecture-2-2-currying

Lambda calculus: Apply in Scala

I imagined this would work. Am I doing something clearly wrong?
val apply = (f: Any => Any, v: Any) => f(v)
val square = (x: Int) => x * x
I imagined apply(square, 10) would result in 100.
But I get an error:
:15: error: type mismatch;
found : Int => Int
required: Any => Any
apply(square, 100)
What I am missing here?
Functions are covariant in their return types, but contravariant in arguments.
Int => Int is a subclass of Int => Any, but not of Any => Int or Any => Any (if it was, you could use in a context where, for example, a String parameter is passed in, and it would not be able to handle it, because it wants an Int).
Cosider this:
val foo: Function[Int, Int] = { x => x * x }
def bar(f: Any => Any)(arg: Any): Any = f(arg)
bar(foo)("foo") //???
The if Int => Int was a subclass of Any => Any, then the last line would be valid. But it cannot be, because it would result in foo being called with a String parameter.
Note, on the other hand, that Any => Int is a subclass of Int => Int (you can use the former anywhere the latter is required). This is what being "contravariant" means.