How to declare implicit arguments in higher order functions? - scala

I want IMPLICIT args in a higher order function, like:
func(arg1) { implicit (x, y) => x * y }
But the compiler says:
error: expected start of definition
val a = func("2", "4") { implicit (x, y) =>
^
java version "1.7.0_40"
Scala code runner version 2.10.2-RC2 -- Copyright 2002-2013, LAMP/EPFL
The runnable sample code:
object Test extends App {
new Test().run
}
class Test {
def run = {
val a = func("2", "4") { (x, y) => // It's OK
x * y
}
println("a: " + a)
val b = gunc("2", "4") { implicit x => { implicit y => // It's OK
x * y
}}
println("b: " + b)
}
def func(x: String, y: String)(f: (Int, Int) => Int) = f(x.toInt, y.toInt)
def gunc(x: String, y: String)(g: Int => Int => Int) = g(x.toInt)(y.toInt)
def hunc(x: String, y: String)(h: Tuple2[Int, Int] => Int) = h((x.toInt, y.toInt))
}
[ADD COMMENT]
I wonder...
We can declare as "implicit x => ..." with one arg.
It seems there is no way to declare two implicit args.

Try adding:
val c = hunc("2", "4") { implicit pair => pair._1 * pair._2 }

When you say implicit y => y * 2 you're not
declaring an implicit argument but mark the function as implicit,
so you make an analog to this:
implicit val f1 = (y: Int) => y * 2
def func1(x: String, y: String)(f: Int => Int) = f(1)
func1("", "")(f1)
When you want to mark a function with two
arguments as implicit you can do it this way:
implicit val f2 = (x: Int, y: Int) => y * 2
def func2(x: String, y: String)(f: (Int, Int) => Int) = f(1, 2)
func2("", "")(f2)
But you cannot do it so:
func2("", "")(implicit (x, y) => x), in this particular case I just don't see any meaning to use implicits.
Also you can see this question, maybe you'll find some useful information there

Related

PartialFunction to partially applied function

I'm trying to do the following:
val divide: PartialFunction[(Int, Int), Int] = {
case (x, y) if (y != 0) => x / y
}
val divide42 = (y: Int) => divide((42, y))
However:
divide42.isDefinedAt(0)
cmd18.sc:1: value isDefinedAt is not a member of Int => Int
val res18 = divide42.isDefinedAt(0)
^Compilation Failed
What is the right way to preserve PartialFunction functionality when going from PartialFunction to partially applied function?
You can use compose to preserve the partial nature:
val divide: PartialFunction[(Int, Int), Int] = {
case (x, y) if (y != 0) => x / y
}
val divide42: PartialFunction[Int, Int] = divide.compose {
case y: Int => (42, y)
}
divide42.isDefinedAt(0) // false
What you wrote doesn't work because it's actually syntactic sugar for the following:
val divide42 = new Function1[Int, Int] {
def apply(y: Int) = divide((42, y))
}
The call to divide inside divide42 is an expression that evaluates to Int (or throw). There is no way to preserve the fact that divide is a partial function with this syntax.
Or explicitly write a new partial function like this but more verbose:
val divide: PartialFunction[(Int, Int), Int] = {
case (x, y) if (y != 0) => x / y
}
val divide42: PartialFunction[Int, Int] = {
case (y: Int) if divide.isDefinedAt((42, y)) => divide((42, y))
}
divide42.isDefinedAt(0) // false
Help the compiler help you, define your types explicitly:
EDIT: I realized that simply changing the types results in the y != 0 part of the function not working as intended. compose addresses this as already mentioned in another answer (which is correct).
val divide: PartialFunction[(Int, Int), Int] = {
case (x, y) if y != 0 => x / y
}
// OLD: val divide42: PartialFunction[Int, Int] = (y: Int) => divide((42, y))
// |- Returns true for `isDefinedAt(0)` which is WRONG!!
// |- If someone could explain why this happens (???), please comment :)
val divide42: PartialFunction[Int, Int] = (y: Int) => divide.compose {
case y: Int => (42, y)
}
divide42.isDefinedAt(0) // False
When you define it as you have, the compiler changes the type of divide42 to Int => Int as opposed to PartialFunction[Int, Int]:
// Implicit type, `Int => Int`
val divide42 = (y: Int) => divide((42, y))
// `isDefinedAt` is not defined for `Int => Int`
divide42.isDefinedAt(0) // !! Error !!
The following seem right to me:
val divide: PartialFunction[(Int, Int), Int] = {
case (x, y) if (y != 0) => x / y
}
val divide42: PartialFunction[Int, Int] = {
case x if (x != 0) => divide(42, x)
}
divide42.isDefinedAt(0) // false
divide42(6) // 7
This both follows the correct definition pattern for PartialFunction and suits the definition of a partially applied function

Partial Functions as an input parameter in scala

I am trying to pass the partial function as input to square function.
What is worng with the following code?
package PartialFunction
object PartialFunctionSum {
val yourConstant = 10
val pf: PartialFunction[(Int, Int), Int] = {
case (x, y) => x + y + yourConstant
}
def square(cb:(Int, Int) => Int): Unit = {
println(cb(5,10))
}
def main(args: Array[String]): Unit= {
square(pf)
}
}
A PartialFunction is a Function1 (a => b) and square needs a Function2 ((a,b) => c)
One way to accomplish what you want to do is changing cb type to Function1.
object PartialFunctionSum extends App {
val yourConstant = 10
val pf: PartialFunction[(Int, Int), Int] = {
case (x, y) => x + y + yourConstant
}
def square(cb:((Int, Int)) => Int): Unit = {
println(cb(5,10))
}
square(pf)
}
Also, anywhere where you need a FunctionX, you can define a PartialFunction, so
square {
case (x, y) => x + y + yourConstant
}
Or transform your PartialFunction in a Function2 with a lambda
square( pf.apply(_, _) )
Anywhere you need a function you can a pass a PartialFunction,
Your problem is with square definition:
def square(cb:(Int, Int) => Int): Unit = {
println(cb(5,10))
}
That means that cb is a function that receives two Ints and returns another Int.
Just change square signature as follows:
def square(cb: ((Int, Int)) => Int): Unit = {
println(cb(5 -> 10))
}
And now your types are compatible and the rest of your code works as you expected.
(Int, Int) => Int is an instance of Function2[Int, Int, Int](a Function that takes 2 Int parameters and returns an Int), not PartialFunction[(Int, Int), Int] (a partial function that takes an input of type (Int, Int) and returns an Int)
If you want to pass your parameters as a Tuple then:
def square(cb:PartialFunction[(Int, Int),Int]): Unit = {
println(cb((5,10)))
}
Example:
def square(cb:PartialFunction[(Int, Int),Int]): Unit = {
println(cb((5,10)))
}
square: (cb: PartialFunction[(Int, Int),Int])Unit
scala> square{case (i, j) => i * j}
50

Calling a function with arguments in a method scala

Suppose I have functions that takes 2 Ints and returns 1 Int:
val sub : (Int, Int) => Int = (x,y) => x - y
val mul : (Int, Int) => Int = (x,y) => x * y
How will I create a method that accepts the above type. I tried:
def test(f:(Int,Int) => Int) : Unit = {
val a = f
println(a)
}
Which does not seem to work.
I guess by "Does not seem to work.", you mean you're trying to call the function and expected an output in the println.
The function works as designed. It's just not the way you probably hoped it will work...
So this for instance will not work:
scala> test(sub(5, 3))
<console>:14: error: type mismatch;
found : Int
required: (Int, Int) => Int
test(sub(5, 3))
But you can modify test() slightly:
scala> def test(f:(Int,Int) => Int, x: Int, y: Int) : Unit = {
| val a = f(x, y)
| println(a)
| }
test: (f: (Int, Int) => Int, x: Int, y: Int)Unit
And call it with the params:
scala> test(sub, 5, 3)
2
scala> test(mul, 5, 3)
15

Scala correct syntax to use underscore("_") in function literal with tuple

I defined a function foo like
def foo(f : (Int, Int) => Int) = f(1,2) // just calling with some default vals
and can invoke it like
foo(_+_)
But when i try to use the same way to invoke a function that takes IntPair(custom type) as param then i receive the error
error: wrong number of parameters; expected = 1
What is the correct syntax to invoke it
Sample Code
type IntPair = (Int, Int)
def sum(f: (IntPair) => Int): (IntPair) => IntPair = {
def iter(pair: IntPair): IntPair = {
val n = readLine()
print(s"$n+")
if (n != "q") {
val (total, accum) = pair
val p: IntPair = (f(n.toInt, total), accum + 1)
iter(p)
} else {
pair
}
}
iter
}
i can invoke like
val f = sum((p : IntPair) => p._1 + p._2) // i want to use here _ syntax
f((0,0))
but not like
val f = sum((_._1 + _._2)) //gives error
scala> def sum(p: ((Int, Int)) => Int) = p((2, 3))
sum: (p: ((Int, Int)) => Int)Int
scala> sum(Function.tupled(_+_))
res4: Int = 5
In Scala parameter lists and tuples are not unified, thus you can't simply pass a tuple to a function that expects multiple arguments.
sum(_._1 + _._2) means sum((x,y) => x._1 + y._2) btw, hence the error.

Why doesn't Function2 have an andThen method?

Why does andThen only exist for single argument functions in Scala?
The following code works:
val double = (x: Int) => x * 2
val timesFour = double andThen double
But why is there no andThen method for multi argument functions?
val multiply = (x: Int, y: Int) => x * y
val multiplyAndDouble = multiply andThen double
<console>:10: error: value andThen is not a member of (Int, Int) => Int
Surely it is trivial to add this method. Is there a reason it been omitted from the standard library?
I have just noticed it is easy to work around with the following:
val multiplyAndDouble = multiply.tupled andThen double
val res = multiplyAndDouble(1, 3) // res = 6
I can't speak as to why Function2 doesn't supply and andThen, but Scalaz defines Functor instances for functions of various arities where map is equivalent to andThen, meaning you could write
val multiplyAndDouble = multiply map double
There is a similar question here:
Scala API 2.10.*: Function2.andThen what happened to?, but there is also no answer. In my opinion it is possible. Here is working example for Scala 2.11.1:
object TestFunction2 {
def main(args: Array[String]): Unit = {
val double = (x: Int) => x * 2
val timesFour = double andThen double
println(timesFour(2)) // prints 8
val multiply = (x: Int, y: Int) => x * y
val multiplyAndDouble = multiply andThen double
println(multiplyAndDouble(1, 3)) // prints 6
}
implicit def toFunc2(function2: Function2[Int, Int, Int]): Func2[Int, Int, Int] = {
new Func2[Int, Int, Int] {
def apply(v1: Int, v2: Int): Int = function2(v1, v2)
}
}
}
trait Func2[-T1, -T2, +R] extends Function2[T1, T2, R] {
def andThen[A](g: R => A): (T1, T2) => A = { (x, y) => g(apply(x, y)) }
}
Another way to write theons's answer is to use:
val multiplyAndDouble = double compose multiply.tupled
val result = multiplyAndDouble(2, 6) // res 24