I have several functions with same input and output types initialized in an object
object Utils {
def f1(value: Int): Double = ???
def f2(value: Int): Double = ???
def f3(value: Int): Double = ???
}
I have a list of higher order values for those functions:
val x = List(Utils.f1, Utils.f2)
How can I use pattern matching to check which functions of those declared in the object are contained in x? I would like to obtain something similar to the following code:
x(0) match {
case Utils.f1 => ...
case Utils.f2 => ...
}
You can't match against functions, it has no meaningfully defined equality.
This will be possible if you make f1, f2, f3 vals. Please notice that upon pattern matching the equality by reference will be used
object Utils {
val f1: Int => Double = _ * 10.0
val f2: Int => Double = _ * 20.0
val f3: Int => Double = _ * 30.0
}
val x: Seq[Int => Double] = List(Utils.f1, Utils.f2)
import Utils._
x(0) match {
case `f1` => println(1)
case `f2` => println(2)
case `f3` => println(3)
}
If you keep f1, f2, f3 defs then
object Utils {
def f1(value: Int): Double = value * 10.0
def f2(value: Int): Double = value * 20.0
def f3(value: Int): Double = value * 30.0
}
val x: Seq[Int => Double] = List(Utils.f1, Utils.f2)
val f1: Int => Double = Utils.f1
val f2: Int => Double = Utils.f2
val f3: Int => Double = Utils.f3
x(0) match {
case `f1` => println(1)
case `f2` => println(2)
case `f3` => println(3)
}
produces MatchError.
Related
I have the following code
object Test {
def bar: String => Double = {
foo[String](_.toDouble)
}
def baz: (Double, Double) => Double = {
foo[(Double, Double)] {
case (d1, d2) => d1 + d2
}
}
def foo[T](f: T => Double): T => Double = {
f
}
}
bar works with no trouble, as expected. I am trying to get a similar thing working with mutli parameter function as one of the inputs, but this doesn't work, because scala sees the foo[(Double, Double)] as a tuple type rather than as a function parameter. Is there any way to tell scala this is a function parameter, rather than a tuple?
the code: https://scastie.scala-lang.org/sCYyU6ziT3mKOhkBiofAaQ
I think the reason this didn't work for you is that you don't access the two separate arguments to the baz method, which need to be packaged into a tuple before calling foo. (I think there's some discussion about being able to convert parameter lists to/from tuples, but that's not currently possible.) Try this definition instead:
object Test {
def bar: String => Double = {
foo[String](_.toDouble)
}
def baz: (Double, Double) => Double = {(p1, p2) =>
foo[(Double, Double)] {
case(d1, d2) => d1 + d2
}((p1, p2))
}
def foo[T](f: T => Double): T => Double = f
}
If I understand your intent correctly, this would be used like this (in the Scala REPL):
scala> Test.bar("5.0")
val res0: Double = 5.0
scala> Test.baz(3.0, 5.0)
val res1: Double = 8.0
However, this seems a little muddled. If bar and baz store method references, then they should be declared val, instead of def:
object Test {
val bar: String => Double = {
foo[String](_.toDouble)
}
val baz: (Double, Double) => Double = {(p1, p2) =>
foo[(Double, Double)] {
case(d1, d2) => d1 + d2
}((p1, p2))
}
def foo[T](f: T => Double): T => Double = f
}
Which has the same usage syntax and produces the same output:
scala> Test.bar("5.0")
val res2: Double = 5.0
scala> Test.baz(3.0, 5.0)
val res3: Double = 8.0
But even that is still fairly clumsy. bar is a reference to a function that takes a String and returns a Double, while baz is a reference to a function that takes two Doubles and returns a Double. So why not just make bar and baz methods in their own right, instead of method references? This would produce:
object Test {
def bar(s: String): Double = foo[String](_.toDouble)(s)
def baz(p1: Double, p2: Double): Double = {
foo[(Double, Double)] {
case(d1, d2) => d1 + d2
}((p1, p2))
}
def foo[T](f: T => Double): T => Double = f
}
which again is used in the same way:
scala> Test.bar("5.0")
val res4: Double = 5.0
scala> Test.baz(3.0, 5.0)
val res5: Double = 8.0
And, of course, foo itself is redundant, since it is identical to its argument. So this could all be written far more simply as just:
object Test {
def bar(s: String): Double = s.toDouble
def baz(p1: Double, p2: Double): Double = p1 + p2
}
Suppose I have to following:
def f1: Int ( or def f1(): Int )
def f2 (x: Int): Int
def f3 (x: Int): Int
def f4: Int
...
...
note: 'Int' here is just an example
I would like to do ....
class Container[T] {
val values = mutable.ListBuffer.empty[T => Int]
def addValue(value: T => Int) = values += v
def doSome(t: T): Int = values.foldLeft[Int](0){ (complete, v) => complete + v(t) }
}
val ContainerWithParam = new Container[Int]
val ContainerWithoutParam = new Container[???]
ContainerWithParam.addValue(f2)
ContainerWithoutParam.addValue(f1)
val result = ContainerWithParam.doSome(1000) + ContainerWithoutParam.doSome(???)
One solution is to use Option[Nothing]
class Container[T] {
val values = mutable.ListBuffer.empty[T => Int]
def addValue(value: T => Int) = values += v
def doSome(t: T): Int = values.foldLeft[Int](0){ (complete, v) => complete + v(t) }
}
def f1(nothing: Option[Nothing]): Int
val ContainerWithoutParam = new Container[Option[Nothing]]
ContainerWithoutParam.doSome(None)
but I think this is not a very clean and nice code...
If def f1: Int = ??? then ...
val containerWithParam = new Container[Int]
val containerWithoutParam = new Container[Unit]
containerWithParam.addValue(f2)
containerWithoutParam.addValue(_ => f1)
val result = containerWithParam.doSome(1000) +
containerWithoutParam.doSome(())
If def f1(): Int = ??? then .addValue(_ => f1()).
The answer from #jwvh is right, but as an alternative you can create a separate class for the case where the function does not have a parameter. This can re-use the original implementation.
class ContainerNoParam {
private val container = new Container[Unit];
def addValue(value: => Int): Unit = container.addValue(_ => value)
def doSome(): Int = container.doSome(())
}
val ContainerWithParam = new Container[Int]
val ContainerWithoutParam = new ContainerNoParam
ContainerWithParam.addValue(f2)
ContainerWithoutParam.addValue(f1)
val result = ContainerWithParam.doSome(1000) + ContainerWithoutParam.doSome()
Is it possible to pattern match on function heads in scala?
For example, can I write something along the lines of:
def myFunction(a:: b:: xs): Int = ???
def myFunction(a:: xs): Int = ???
def myFunction(List.empty): Int = ???
You can use partial functions for this case. Example:
val myFunctionCase1: PartialFunction[List[Int], Int] = {
case a :: b :: xs => ???
}
val myFunctionCase2: PartialFunction[List[Int], Int] = {
case a :: xs => ???
}
val myFunctionCase3: PartialFunction[List[Int], Int] = {
case Nil => ???
}
// compose functions
val myFunction: List[Int] => Int =
myFunctionCase1 orElse myFunctionCase2 orElse myFunctionCase3
Usage examples:
myFunctionCase1(List(1,2,3)) // invoke
myFunctionCase1(List(1)) // throw MatchError
myFunctionCase2(List(1)) // invoke
...
myFunction(List(1,2,3))
myFunction(List(1))
myFunction(Nil)
...
It's weird but my code prints u. Any ideas why does it do such stuff?
object PF extends App {
val f1: PartialFunction[Int, String] = {
case x: Int if x % 2 == 0 => "2"
}
val f2: PartialFunction[Int, String] = {
case x: Int if x % 3 == 0 => "3"
}
val f3: PartialFunction[Int, String] = {
case x: Int if x % 5 == 0 => "5"
}
val result = f1.orElse(f2.orElse(f3.orElse("<undef>")))
println(result.apply(1))
}
Your code interprets the string "" as PartialFunction:
val result: PartialFunction[Int, String] = "<undef>"
result.apply(1) // second character of "<undef>" --> u
This happens through an implicit conversion from String to WrappedString which is a subtype of Seq[Char]. Further, Seq[T] is a subtype of PartialFunction[Int, T] (given an index, get an element of the Seq if it exists).
The last line reaches this case, since 1 is not divisible by any of 2,3,5 (so it falls through f1, f2 and f3).
What you would want instead is applyOrElse:
val fun = f1 orElse f2 orElse f3
fun.applyOrElse(1, "<undef>") // --> "<undef>"
Alternatively, you can specify a fallback partial function:
val result = f1 orElse f2 orElse f3 orElse {
case _ => "<undef>"
}
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