partialfunction

Scala missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5)

天大地大妈咪最大 提交于 2021-01-28 07:12:41
问题 I have the following snippet I need to complete for an assignment. To fulfill the asignment I have to correctly replace the comments /*fulfill ...*/ . However I tried my best and I am still getting an missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5) error. I found similar questions related to this error. However I could not derive a solution for my paticular problem of those answers. So the target is to check whether the

Scala regex and partial functions

☆樱花仙子☆ 提交于 2020-05-18 02:42:25
问题 I want to use Scala's collect function with a regular expression. Ideally I'd like to collect only those terms that match the regular expression. I've so far implemented the following which works fine val regex = "(^([^:]+):([^:]+):([^:]+):([+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?)$".r <other_code>.collect{case x: String if regex.pattern.matcher(x).matches => x match { case regex(feature, hash, value, weight) => (feature.split("\\^"), weight.toDouble) } } This seems to have an extra step though

Combine a PartialFunction with a regular function

孤街醉人 提交于 2020-01-14 13:56:09
问题 So, suppose, I want to provide a "catch all" fall back for a PartialFunction : val foo: PartialFunction[Int, String] = { case 1 => "foo" } val withDefault = foo orElse { _.toString } This does not compile: missing parameter type for expanded function ((x$1) => x$1.toString) . This: val withDefault = foo orElse { case x: Int => x.toString } Does not compile either (same error). This: val withDefault = foo orElse { (x: Int) => x.toString } fails with type mismatch; found : Int => String;

Using Tuples in map, flatmap,… partial functions

自闭症网瘾萝莉.ら 提交于 2019-12-29 09:05:05
问题 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? 回答1: The error message with 2.11 is more explanatory: scala> l map { (b, n) => b + n } <console>:9: error:

Using Tuples in map, flatmap,… partial functions

倖福魔咒の 提交于 2019-12-29 09:04:10
问题 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? 回答1: The error message with 2.11 is more explanatory: scala> l map { (b, n) => b + n } <console>:9: error:

How does orElse work on PartialFunctions

久未见 提交于 2019-12-18 15:58: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

Anonymous PartialFunction syntax

妖精的绣舞 提交于 2019-12-12 11:47:03
问题 I asked this question earlier: Combine a PartialFunction with a regular function and then realized, that I haven't actually asked it right. So, here goes another attempt. If I do this: val foo = PartialFunction[Int, String] { case 1 => "foo" } val bar = foo orElse { case x => x.toString } it does not compile: error: missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5) Expected type was: PartialFunction[?,?] But this works fine

PartialFunction and MatchError

耗尽温柔 提交于 2019-12-10 20:46:32
问题 There are two ways to define PF: 1) with literal case {} syntax and 2) as explicit class. I need the following function throw a MatchError, but in the second case that doesn't happen. 1) with case val test: PartialFunction[Int, String] = { case x if x > 100 => x.toString } 2) as class val test = new PartialFunction[Int, String] { def isDefinedAt(x: Int) = x > 100 def apply(x: Int) = x.toString } Should i, in the seconds case, manually call isDefinedAt , shouldn't it be called implicitly by

Is there a nicer way of lifting a PartialFunction in Scala?

梦想的初衷 提交于 2019-12-10 18:29:42
问题 I occasionally come across the following pattern, where I essentially have a PartialFunction[SomeType,AnotherType] , and want to treat it as a Function[SomeType,Option[AnotherType] , eg: def f(s:SomeType):Option[AnotherType] = s match { case s1:SubType1 => Some(AnotherType(s1.whatever)) case s2:SubType2 => Some(AnotherType(s2.whatever)) case _ => None } Is there a way to write the above function in a way that avoids the default case and wrapping the result in Some where it's defined? The best

Can't put PartialFunction in scala class constructor

眉间皱痕 提交于 2019-12-10 13:59:01
问题 There appears to be a restriction that you can't use PartialFunction literals in class constructors: scala> case class X(a: PartialFunction[Any, Any]) { def this() = this({case x => x}) } <console>:7: error: Implementation restriction: <$anon: Any => Any> requires premature access to class X. case class X(a: PartialFunction[Any, Any]) { def this() = this({ case x => x}) } My first question is why does a partial function literal need access to "this". My second question/observation is that in