partialfunction

Scala, partial functions

拈花ヽ惹草 提交于 2019-12-10 03:24:15
问题 Is there any way to create a PartialFunction except through the case statement? I'm curious, because I'd like to express the following (scala pseudo ahead!)... val bi = BigInt(_) if (bi.isValidInt) bi.intValue ... as a partial function, and doing val toInt : PartialFunction[String, Int] = { case s if BigInt(s).isValidInt => BigInt(s).intValue } seems redundant since I create a BigInt twice. 回答1: Not sure I understand the question. But here's my attempt: Why not create an extractor? object

Magic PartialFunction in Scala

笑着哭i 提交于 2019-12-07 16:56:00
问题 I don't think this code should work, but it does (in Scala 2.10): scala> ((i: Int) => i.toString match { | case s if s.length == 2 => "A two digit number" | case s if s.length == 3 => "A three digit number" | }): PartialFunction[Int,String] res0: PartialFunction[Int,String] = <function1> // other interactions omitted scala> res0.orElse(PartialFunction((i: Int) => i.toString)) res5: PartialFunction[Int,String] = <function1> scala> res5(1) res6: String = 1 How does it work? I would expect a

Is PartialFunction orElse looser on its type bounds than it should be?

我的未来我决定 提交于 2019-12-07 04:12:33
问题 Let's define a PartialFunction[String, String] and a PartialFunction[Any, String] Now, given the definition of orElse def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] I would expect not to be able to compose the the two, since A → String A1 → Any and therefore the bound A1 <: A (i.e. Any <: String ) doesn't hold. Unexpectedly, I can compose them and obtain a PartialFunction[String, String] defined on the whole String domain. Here's an example: val a:

Magic PartialFunction in Scala

帅比萌擦擦* 提交于 2019-12-06 02:25:33
I don't think this code should work, but it does (in Scala 2.10): scala> ((i: Int) => i.toString match { | case s if s.length == 2 => "A two digit number" | case s if s.length == 3 => "A three digit number" | }): PartialFunction[Int,String] res0: PartialFunction[Int,String] = <function1> // other interactions omitted scala> res0.orElse(PartialFunction((i: Int) => i.toString)) res5: PartialFunction[Int,String] = <function1> scala> res5(1) res6: String = 1 How does it work? I would expect a MatchError to be thrown inside res0 . The Scala language specification does not seem to explicitly

Is PartialFunction orElse looser on its type bounds than it should be?

岁酱吖の 提交于 2019-12-05 09:14:15
Let's define a PartialFunction[String, String] and a PartialFunction[Any, String] Now, given the definition of orElse def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] I would expect not to be able to compose the the two, since A → String A1 → Any and therefore the bound A1 <: A (i.e. Any <: String ) doesn't hold. Unexpectedly, I can compose them and obtain a PartialFunction[String, String] defined on the whole String domain. Here's an example: val a: PartialFunction[String, String] = { case "someString" => "some other string" } // a: PartialFunction[String

Scala, partial functions

自古美人都是妖i 提交于 2019-12-05 03:32:00
Is there any way to create a PartialFunction except through the case statement? I'm curious, because I'd like to express the following (scala pseudo ahead!)... val bi = BigInt(_) if (bi.isValidInt) bi.intValue ... as a partial function, and doing val toInt : PartialFunction[String, Int] = { case s if BigInt(s).isValidInt => BigInt(s).intValue } seems redundant since I create a BigInt twice. Not sure I understand the question. But here's my attempt: Why not create an extractor? object ValidBigInt { def unapply(s: String): Option[Int] = { val bi = BigInt(s) if (bi.isValidInt) Some(bi.intValue)

What is the easiest way to implement a Scala PartialFunction in Java?

泄露秘密 提交于 2019-12-04 18:46:51
问题 For interoperability, I need to pass a Scala PartialFunction from Java code. For Function (Function1 and so on), there is AbstractFunction that I can subclass with an anonymous type, but what would be the easiest way of doing the same for PartialFunction? In this case, I would be happy to have it being a "complete" function in Java, appearing defined for all values, but typed as a PartialFunction. 回答1: If you can use Twitter Util library, it has a class specifically for this: http://twitter

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

六月ゝ 毕业季﹏ 提交于 2019-12-03 18:00: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

Compose partial functions

寵の児 提交于 2019-12-03 12:27:32
问题 I have two PartialFunctions f and g . They have no side effects and are quick to execute. What's the best way to compose them into another partial function h such that h.isDefinedAt(x) iff f.isDefinedAt(x) && g.isDefinedAt(f(x)) ? It's also OK if h is a function returning an Option rather than a partial function. I'm disappointed that f andThen g does not do what I want: scala> val f = Map("a"->1, "b"->2) f: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2) scala> val g = Map(1

What is the easiest way to implement a Scala PartialFunction in Java?

≡放荡痞女 提交于 2019-12-03 11:06:30
For interoperability, I need to pass a Scala PartialFunction from Java code. For Function (Function1 and so on), there is AbstractFunction that I can subclass with an anonymous type, but what would be the easiest way of doing the same for PartialFunction? In this case, I would be happy to have it being a "complete" function in Java, appearing defined for all values, but typed as a PartialFunction. If you can use Twitter Util library, it has a class specifically for this: http://twitter.github.com/util/util-core/target/site/doc/main/api/com/twitter/util/Function.html which is basically the same