val cond: (Int, Int) => Boolean = (...) what does this scala code mean? [closed] - scala

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm confused about the first line of code.
And what's the difference between the first line and the second line?
val cond: (Int, Int) => Boolean = (...) //confused
val cond = (x: Int, y: Int) => x > y //anonymous function

It can be a bit daunting at first, but all Scala declarations are the same shape:
val <name>[: <type>] = <value>
If the type is not there the compiler will set it to the type of the value
So the first case breaks down like this:
The name is cond
The type is (Int, Int) => Boolean
The value is (...)
The second case breaks down like this:
The name is cond
The value is (x: Int, y: Int) => x > y
The type is inferred to be (Int, Int) => Boolean
In both cases cond is a function that takes two Ints and returns a Boolean.

Related

How do you use a method that takes a function as parameter in Scala? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
val list = List.range(0, 10)
def filter (lst: List[Int], fn: (Int) => Boolean): List[Int] = {
var res:List[Int] = Nil
lst.foreach ((x:Int) => if (fn(x)) res = x::res)
return res.reverse
}
The method filter takes 2 arguments, first is a list and second is a function which produce a boolean argument.
I want to use this method to get only the even numbers from list.
This just needs a simple lambda function:
filter(list, _ % 2 == 0)
This is equivalent to
filter(list, x => x % 2 == 0)
or
def isEven(x: Int) = x % 2 == 0
filter(list, isEven)

How to take a constant Integer as input in Partial Function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to scala, I have a use case where I want to define a partial function to add three numbers in which one number is constant and two
numbers can be passed as inputs and define another method which can take the partial
function as input and gives its cube as result.
Well... That depends on where is your constant coming from?
Choice 1 - Your function forms a closure with a constant present in scope.
val yourConstant = 10
val pf: PartialFunction[(Int, Int), Int] = {
case (x, y) => x + y + yourConstant
}
pf((5, 10))
Choice 2 - Your function has a local constant.
val pf: PartialFunction[(Int, Int), Int] = {
case (x, y) => x + y + 10
}
pf((5, 10))
Also, as many others pointed out - this does not look like a use case of partial function. Are you sure that you want a Partial Function and not a partially applied function ?
if you were looking for a partially applied function then,
// first you need a curried function
// Curries function are function which can take parameters in steps to build intermidatary functions.
def normalDef(c: Int)(x: Int, y: Int): Int = c + y + x
// normalDef: normalDef[](val c: Int)(val x: Int,val y: Int) => Int
// now you can "partially apply" this "curried" function to your partially applied function
val addTo10PartiallyApplied = normalDef(10) _
// addTo10PartiallyApplied: (Int, Int) => Int = $Lambda$1240/1924827254#46202553
val total = addTo10PartiallyApplied(1, 2)
// total: Int = 13
The following partial function adds 12345 to each number in the tuple passed to it
scala> val addConstantTo: PartialFunction[(Int, Int), Int] = {
| case (a, b) => a + b + 12345
| }
addConstantTo: PartialFunction[(Int, Int),Int] = <function1>
scala> addConstantTo((12, 34))
res4: Int = 12391
This expands on the concept, by programmatically defining a partial function which adds any number to the elements of a tuple:
scala> def addTo(c: Int): PartialFunction[(Int, Int), Int] = {
| case (a, b) => a + b + c
| }
addTo: (c: Int)PartialFunction[(Int, Int),Int]
scala> val pf = addTo(3)
pf: PartialFunction[(Int, Int),Int] = <function1>
scala> pf((1, 2))
res5: Int = 6
Let that sink in for a bit :)

Helper method with accumulators in Scala [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Below is an example used to illustrate how to use immutable variables. (1) What function is this method performing and (2) which portion is considered the accumulator?
def fib(n: Int): Int = {
def fibIter(i: Int, a: Int, b: Int): Int =
if (i == n) a else fibIter(i+1, b, a+b)
fibIter(0, 0, 1)
}
As mentioned by #jwvh in his comment, this is a function for computing the nth term of the Fibonacci sequence.
This particular implementation is tail recursive and uses the inner function fibIter as an "accumulator." Often in order to code a tail recursive implementation of a recursive function, it is useful to define an inner tail recursive function that accumulates the desired result. The outer function will the call this inner function with some default params. Sometimes this inner function is called "accumulate" or "go" or "loop", etc.
Here would be my preferred implementation of a Fibonacci similar to the above...
def fibonacci(n: Int): Int = {
#annotation.tailrec
def iterate(i: Int, a: Int, b: Int): Int =
if (i == n) a else iterate(i+1, b, a+b)
iterate(0, 0, 1)
}
...here I prefer to call the inner function iterate because this is a more accurate description of what the function is doing (i.e., it isn't really accumulating anything).

Scala: Difference between function declarations [duplicate]

This question already has answers here:
Differences between these three ways of defining a function in Scala
(3 answers)
Closed 6 years ago.
I am new to Scala, below are three function declarations that are confusing to me:
First
scala> def sum = (a: Int, b: Int) => a + b
sum: (Int, Int) => Int
Second
scala> def sum (a: Int, b: Int) = a + b
sum: (a: Int, b: Int)Int
Third
scala> val sum = (a: Int, b: Int) => a + b
sum: (Int, Int) => Int = <function2>
I know that the second one is method, third one is a function literal. My confusion is with the first declaration, is it a normal function or is it a some other syntax that I do not understand? An explanation would be appreciated.
EDIT
Also, when I try to create a partially applied function using the first declaration I get this:
scala> val anotherSum = sum _
anotherSum: () => (Int, Int) => Int = <function0>
Which I expected to be of type Function2 as sum has 2 parameters.
The first and second declarations declare different things. It's not the same thing with different syntax.
scala> def sum = (a: Int, b: Int) => a + b
sum: (Int, Int) => Int
Here, you define a method named sum that takes no parameters and returns a function that takes two Int parameters and returns an Int.
So, sum is a method that returns a function that adds two numbers.
scala> def sum (a: Int, b: Int) = a + b
sum: (a: Int, b: Int)Int
Here, you define a method named sum that takes two Int parameters and that returns an Int.
So, sum is a method that adds two numbers.
The difference is that in the first version, sum takes no parameters and returns a function, and in the second, sum takes two parameters and returns an Int. Two very different things!
The third:
scala> val sum = (a: Int, b: Int) => a + b
sum: (Int, Int) => Int = <function2>
Here you define a value named sum that is a Function2, in other words, a function that takes two Int parameters and returns an Int.
So, sum is a value that is of type Function2, and refers to a function that adds two numbers.
Extra:
scala> val anotherSum = sum _
anotherSum: () => (Int, Int) => Int = <function0>
Look carefully at the type. anotherSum is a function that takes no parameters that returns a function that takes two Int parameters that returns an Int.
You are not partially applying anything here. Section 6.7 Method Values of the Scala Language Specification explains what happens here.

Folding Curried Functions in Scala [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Applying an argument list to curried function using foldLeft in Scala
Consider:
val f = (i: Int, j: Int, k: Int, l: Int) => i + j + k + l
Because one can do this:
f.curried.apply(1).apply(2).apply(3).apply(4)
It's easy to fall into the trap of trying this:
List(1, 2, 3, 4).foldLeft(f.curried) { (fs, e) => fs.apply(e) }
However, the B parameter in the fold changes type once one applies an argument to it. In this example, the first iteration would change from a (Int) => (Int) => (Int) => (Int) => Int to a (Int) => (Int) => (Int) => Int.
Question: How to solve this?
You should be able to do this with an HList - see http://apocalisp.wordpress.com/2010/07/08/type-level-programming-in-scala-part-6b-hlist%C2%A0folds/ for an example of building fold functions over heterogenous lists.
Edit - hbatista's link in the comment above provides a full solution doing precisely this.