partialfunction

Partial function application prematurely runs codeblock when used with underscore

江枫思渺然 提交于 2019-12-01 00:11:12
问题 Given: def save(f: => Any)(run:Boolean) { if (run) { println("running f"); f } else println("not running f") } I can call it with: save("test")(true) -> running f save("test")(false) -> not running f save(throw new RuntimeException("boom!"))(false) -> not running f save(throw new RuntimeException("boom!"))(true) -> running f and then exception thrown Here's the curious behaviour with partial application: save(throw new RuntimeException("boom!"))(_) -> (Boolean) => Unit = <function1> //as

How does orElse work on PartialFunctions

筅森魡賤 提交于 2019-11-30 13:09:25
I am getting very bizarre behavior (at least it seems to me) with the orElse method defined on PartialFunction It would seem to me that: val a = PartialFunction[String, Unit] { case "hello" => println("Bye") } val b: PartialFunction[Any, Unit] = a.orElse(PartialFunction.empty[Any, Unit]) a("hello") // "Bye" a("bogus") // MatchError b("bogus") // Nothing b(true) // Nothing makes sense but this is not how it is behaving and I am having a lot of trouble understanding why as the types signatures seem to indicate what I exposed above. Here is a transcript of what I am observing with Scala 2.11.2:

Using Tuples in map, flatmap,… partial functions

别等时光非礼了梦想. 提交于 2019-11-29 14:55:01
If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { t => t._1 + t._2 } It's ok. If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { case (b, n) => b + n } It's ok too. But if I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { (b, n) => b + n } It will not work. Why should I use "case" keyword to use named tuples? The error message with 2.11 is more explanatory: scala> l map { (b, n) => b + n } <console>:9: error: missing parameter type Note: The expected type requires a one-argument function accepting a 2-Tuple. Consider

Chaining PartialFunctions with andThen in Scala

社会主义新天地 提交于 2019-11-29 13:39:19
Let us reuse examples from Daily scala : type PF = PartialFunction[Int,Int] val pf1 : PF = {case 1 => 2} val pf2 : PF = {case 2 => 3} and let us add: val pf3 : PF = {case 3 => 4} andThen works as expected here: pf1 andThen pf2 isDefinedAt(x) returns true iff x == 1 (actually, pf2 does not need to be a PartialFunction at all) However, I expected that: pf1 andThen pf3 isDefinedAt(x) would return false for all x (i.e., iff pf1 is defined, check for pf3), but it does not and only validates pf1. In the end, pf1 andThen pf3 lift(x) always result in a MatchError. I would prefer to get None… I can

How to convert X => Option[R] to PartialFunction[X,R]

女生的网名这么多〃 提交于 2019-11-29 05:30:43
As long as we have a PartialFunction[X,R] it's very easy to convert it to a function returning Option[R] , e.g. def pfToOptf[X, R](f: PartialFunction[X,R])(x: X) = if (f.isDefinedAt(x)) Some(f(x)) else None However, what if the task is opposite: suppose I have a function f getting X as an argument and returning Option[R] as a result. And I want to make a PartialFunction[X,R] out of it. What is the best way? What I've come up with looks pretty ugly to my taste: def optfToPf[X,R](f: X => Option[R]) : PartialFunction[X,R] = { object extractor { def unapply(x: X): Option[R] = f(x) } { case

Chaining PartialFunctions with andThen in Scala

一曲冷凌霜 提交于 2019-11-28 07:14:41
问题 Let us reuse examples from Daily scala : type PF = PartialFunction[Int,Int] val pf1 : PF = {case 1 => 2} val pf2 : PF = {case 2 => 3} and let us add: val pf3 : PF = {case 3 => 4} andThen works as expected here: pf1 andThen pf2 isDefinedAt(x) returns true iff x == 1 (actually, pf2 does not need to be a PartialFunction at all) However, I expected that: pf1 andThen pf3 isDefinedAt(x) would return false for all x (i.e., iff pf1 is defined, check for pf3), but it does not and only validates pf1.