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].
Questions tagged [partialfunction]
120 questions
55
votes
7 answers
Empty partial function in Scala
It seems to me like the { case ... => ... } syntax for partial functions require at least one case:
scala> val pf: PartialFunction[String, String] = { case "a" => "b" }
pf: PartialFunction[String,String] =
scala> val pf:…

aioobe
- 413,195
- 112
- 811
- 826
24
votes
3 answers
How to convert X => Option[R] to PartialFunction[X,R]
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…

Alexander Azarov
- 12,971
- 2
- 50
- 54
16
votes
2 answers
Compose partial functions
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…

tba
- 6,229
- 8
- 43
- 63
14
votes
3 answers
Scala PartialFunction can be Monoid?
I thought PartialFunction can be Monoid. Is my thought process correct ?
For example,
import scalaz._
import scala.{PartialFunction => -->}
implicit def partialFunctionSemigroup[A,B]:Semigroup[A-->B] = new Semigroup[A-->B]{
def append(s1: A-->B,…

Kenji Yoshida
- 3,108
- 24
- 39
14
votes
1 answer
How can I easily define more complex PartialFunctions in Scala?
PartialFunctions
In Scala, a PartialFunction is, in short, a function that additionally defines an isDefinedAt method.
It is easy to define partial functions with a series of case statement. A trivial example would be, e.g.:
scala> val pf:…

Jean-Philippe Pellet
- 59,296
- 21
- 173
- 234
12
votes
3 answers
What is the easiest way to implement a Scala PartialFunction in Java?
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…

SoftMemes
- 5,602
- 4
- 32
- 61
11
votes
3 answers
Chaining PartialFunctions with andThen in Scala
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…

scand1sk
- 1,114
- 11
- 25
10
votes
3 answers
How does orElse work on PartialFunctions
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] =…

jedesah
- 2,983
- 2
- 17
- 29
9
votes
2 answers
Partial function application prematurely runs codeblock when used with underscore
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…

ssanj
- 2,169
- 3
- 19
- 28
9
votes
1 answer
How can I convert scala Map to a partial function
Currently I use the following snippet of code:
private val aMap = Map(
"J" -> Journey,
"T" -> Training
)
def partialFunction = {
case x if aMap isDefinedAt x => aMap(x)
}
It seems to me that Maps naturally should define a partial function.…

Mequrel
- 727
- 6
- 12
9
votes
4 answers
How exactly "case" works in partial functions in Scala?
I am just starting my seemingly steep learning curve with Scala and can't quite grasp how "case" works in partial functions exactly.
I looked at the definition of PartialFunction itself, and there I see a sample like the following:
val isEven:…

Alex N.
- 14,805
- 10
- 46
- 54
8
votes
5 answers
Scala, partial functions
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…

aioobe
- 413,195
- 112
- 811
- 826
8
votes
5 answers
Scope of variables inside scala’s case guard statement
For lift development, I sometimes need to use match–case statements like the following. (Rewritten to plain scala for easier understanding.) One note to them: These are actually different partial functions, defined in different parts of the code, so…

Debilski
- 66,976
- 12
- 110
- 133
8
votes
2 answers
When is a scala partial function not a partial function?
While creating a map of String to partial functions I ran into unexpected behavior. When I create a partial function as a map element it works fine. When I allocate to a val it invokes instead. Trying to invoke the check generates an error. Is…

Fred Haslam
- 8,873
- 5
- 31
- 31
7
votes
2 answers
Can't put PartialFunction in scala class constructor
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}) }
:7: error: Implementation restriction: <$anon: Any…

Yona Appletree
- 8,801
- 6
- 35
- 52