Questions tagged [partialfunction]

Questions about the Scala's PartialFunction type. For questions about the broader concept of partial functions (i.e. functions not defined for all of their inputs), use [partial-functions]. For questions about partially applied functions, use [partial-application].

120 questions
2
votes
0 answers

A toArray function that breaks on primitive type, but works fine when written as a PartialFunction?

Playing around with a reflection-base code I've come a cross a situation where I want to convert a sequence into array. The catch is that the only type information available is in the form of a runtime.universe.Type, and the sequence itself is of…
Worakarn Isaratham
  • 1,034
  • 1
  • 9
  • 16
2
votes
3 answers

How to convert one partial function to another?

Suppose I've got partial function parf val parf: PartialFunction[Int, String] = { case 0 => "!!!" } Now I've got also case class A(x: Int) and I need a function to transform PartialFunction[Int, String] to PartialFunction[A, String]: def foo(pf:…
Michael
  • 41,026
  • 70
  • 193
  • 341
2
votes
2 answers

How to return early in a pattern match of akka actor receive

Tried googling variations on this trivial question but didn't get an answer... Basically I have a pattern match in my receive method. In some cases I want to break early from the receive handling override def receive = { case blah => { …
Avba
  • 14,822
  • 20
  • 92
  • 192
2
votes
2 answers

What is the reference of the object that a partial function matches on?

Looking at this function as an example: def receive = { case "test" => log.info("received test") case _ => log.info("received unknown message") } What object is being matched on? On the right hand side of the arrows, how can I refer to the…
Nelson
  • 2,972
  • 3
  • 23
  • 34
2
votes
1 answer

What is correct way to use operator orElse in Scala?

I want to write two services, and then use orElse to make the two services combine together, which means service_one or service_two. They are all PartialFunctions. The service one is: val usersService = HttpService { case request @ GET -> Root /…
user504909
  • 9,119
  • 12
  • 60
  • 109
2
votes
3 answers

Variable arguments with partial applied functions

I have a compilation problem on the following code. object Main { def main(args:Array[String]) = { def collectBigger(median:Int)(values:Int*) = values.filter { _ > median } val passedRanks = collectBigger(5)_ //this compiles …
raisercostin
  • 8,777
  • 5
  • 67
  • 76
2
votes
1 answer

Scala PartialFunction Stackoverflow

I'm developing a Scala / Python library called PySpark Cassandra. In it I have to deal with objects serialized Python objects in the pickle format when e.g. saving data. I have a job which fails with a stackoverlfow: org.apache.spark.SparkException:…
2
votes
2 answers

Scala : Overloaded function with PartialFunction in argument

I've just come across a strange problem while trying to overload a function using a partial function : class Foo { def bar(pf: PartialFunction[String, Int]): Foo = ??? def bar(zd: Int): Foo = ??? def zed(pf: PartialFunction[String, Int]): Foo…
Francis Toth
  • 1,595
  • 1
  • 11
  • 23
2
votes
2 answers

inexplicable for-comprehension result in Scala

I understand for-expression is translated into map and flatMap. But I found something that I can not explain and need your helps. Here are two toy examples: for { None <- List(Option(1),None) } yield 0 //res0: List[Int] = List(0, 0) Q1: Why…
2
votes
1 answer

Scala regex + partial function unapply performance

Assume that I have a code like: val pf: PartialFunction[String, Unit] = "string" match { case regex(g1, g2, _*) => function(g1, g2) } pf has methods isDefinedAt and apply. Will the regex search be evaluated once, in isDefinedAt point, or…
dveim
  • 3,381
  • 2
  • 21
  • 31
2
votes
1 answer

Scala function partial application

I'm trying to understand how function partial application works in Scala. To do that, I've built this simple code: object Test extends App { myCustomConcat("General", "Public", "License") foreach print …
nolanofra
  • 555
  • 2
  • 7
  • 14
2
votes
2 answers

Collect results of multiple partial functions at single value?

Suppose I have some partial functions that may have overlapping domains: val funcs: Seq[PartialFunction[Any, Int]] = Vector( { case i: Int if i % 2 == 0 => i*2 } , { case i: Int if i % 2 == 1 => i*3 } , { case i: Int if i % 6 == 0 => i*5 } ) I want…
Arkadiy Kukarkin
  • 2,153
  • 14
  • 24
2
votes
1 answer

How to use PartialFunction.applyOrElse

I have a PartialFuncton[Throwable,Future[Result]] called errorMap to transform an throwable to a result or failed future. I can do it via lift and getOrElse like this: val x: Future[Result] = errorMap.lift(e).getOrElse(Future.failed(e)) I figure…
Arne Claassen
  • 14,088
  • 5
  • 67
  • 106
2
votes
1 answer

How to mix PartialFunction input parameter with output

What is the best way to mix a function input parameter with the output. Here is my current code : def zip[A,B](f: A => B) : A => (A, B) = (a: A) => (a, f(a)) def zip[A,B](pf: PartialFunction[A,B]) : PartialFunction[A, (A, B)] = { case a if…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
2
votes
1 answer

Scala: 'missing parameter type' when calling scala macro with a PartialFunction reify

The compiler is throwing me a 'Missing parameter type'. After cornering the problem I've realized that when chaining Partial Functions you need to be explicit about the types or the compiler will throw the mentioned error. Now, do you guys know if…
ernestRC
  • 35
  • 1
  • 6