Scala Curry-Uncurry A Function - scala

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 = ???

Related

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.

Returning an anonymous function/lambda that is a curried function

Just trying to get a handle on some FP patterns.
Can someone show and explain a function that returns an anonymous method/lambda which is also a curried function?
Here is an example:
val foo: Int => Int => Int = x => y => x + y
Function foo has a signature of Int => Int => Int which means that calling it for some argument (what is also called partial application), it returns another function of signature Int => Int:
val bar: Int => Int = foo(1)
That you can call following way:
val result = bar(2)
result == 3
Can someone show and explain a function that returns an anonymous method/lambda which is also a curried function?
One of the easy ways to see it is to create a currried sum method:
def sum(x: Int): Int => Int = y => x + y
And call it:
val curried: Int => Int = sum(1)
println(curried(2)) // will yield 3
If you want to transform the method to a function, you can use eta expansion:
val sumFunc: Int => Int => Int = sum _
println(sumFunc(1)(2))

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.

How currying works

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.

What is the purpose of outer and inner function parameters in Scala?

In the following code:
def product(f: Int => Int)(a:Int, b:Int): Int =
if (a > b) 1
else f(a) * product(f)(a + 1, b)
The parameters a and b are passed to the inner function, but you could write exactly the same function definition like so:
def product(f: Int => Int, a:Int, b:Int): Int =
if (a > b) 1
else f(a) * product(f, a + 1, b)
So what is the purpose of separating the parameters? In other words, why do this:
(f: Int => Int)(a:Int, b:Int)
when you can more clearly write:
(f: Int => Int, a:Int, b:Int)
Another feature of multiple parameters lists is partial application:
def sum3(a: Int)(b: Int)(c: Int): Int = a + b + c
val g: Int => Int => Int = sum3(10) _
val h: Int => Int = g(20)
val r: Int = h(30) // 10 + 20 + 30 = 60
You can partially apply a function and obtain another function which is equivalent to the original one but with one of the arguments fixed. _ after sum3(10) is needed because sum3 is a method, not a function, and _ converts methods to functions.
This is very useful when you are using higher-order functions:
def adder(x: Int)(y: Int) = x + y
Seq(1, 2, 3, 4) map adder(10) // Seq(11, 12, 13, 14)
When partially applied method/function is used as an argument of a higher-order call, _ is not needed, and the syntax becomes very succinct.
Another use case of this feature is that if you want to create a control structure that looks like it's built into Scala programming language itself.
For example, I could write an control structure named times which help me execute the code block exactly n times by the following method definition:
// since block is call by name, it will not be evaluate when you call it.
def times(n: Int)(block: => Any): Unit = {
for (i <- 0 until n) {
block // evaluate(execute) block
}
}
// Now I can use the times method like a control structure
times(5) {
println("Hello World");
}
It depends whether there is implicit parameter (which can't be properly mixed with plain ones)...
def foo(i: Int)(implicit p: P): Foo
... and the way you want to call it ...
def foo1(a: Int)(b: Int => Boolean): Boolean
foo1(9) { i => false }
def foo2(a: Int, b: Int => Boolean): Boolean
foo2(9, { i => false })