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
2
votes
1 answer
Adding new arguments to a partial function
Is there some simple way how to add new arguments to a partial function, so that resulting function is defined in the same domain as before (new arguments have no influence on its partiality)? Following code works, but seems a bit verbose.
val…

Suma
- 33,181
- 16
- 123
- 191
2
votes
1 answer
How to partially apply case class with type parameter in Scala
So I have a tuple that I want to pass as the parameters for a case class in Scala. For case classes without type parameters, this is easy, as I can do:
scala> case class Foo(a: Int, b: Int)
defined class Foo
scala> (Foo.apply _)
res0: (Int,…

cdmckay
- 31,832
- 25
- 83
- 114
2
votes
1 answer
How to avoid explicit .isDefinedAt() call in pattern matching guard
I have some Akka-based actor system where multiple kind of actors is based on same template due to a fact that those actors only differ by a type of response value. For example:
final case class Request(data: Any)
final case class Response(data:…

Seigert
- 311
- 3
- 11
1
vote
2 answers
Partial Function pattern match split into a class and a trait
Lift uses a PartialFunction on their implementation of Comet Actors, and you usually end up with this on your class:
override def lowPriority: PartialFunction[Any,Unit] = {
case MyCaseClass1(a) => do something here
case MyCaseClass2(a) …

fmpwizard
- 2,758
- 1
- 22
- 24
1
vote
2 answers
partialFunction in collectFirst
I have a sequence of some objects and I want to collect the first
element for which another function returns Some()
For now, my code works this way:
mySeq.collectFirst{
case elem if doSmth(elem).nonEmpty =>
(doSmth(elem).get, elem)
}
Is…

D Artem
- 41
- 5
1
vote
1 answer
Partial function in scala
I see the following code in several places depicting the use of partial functions in Scala.
val divide: PartialFunction[Int, Int] = {
case d: Int if d != 0 => 42 / d
}
Here, divide is a variable whose type of is PartialFunction[Int,Int] which…

user3103957
- 636
- 4
- 16
1
vote
3 answers
How to check if function is partial in Scala?
I have a method that receives a function, but that function may be partial, in such case I don't want it to fail with MatchError.
def doSomething[X,Y](opt:Option[X])(f:X=>Y)={
f match {
case p:PartialFunction[X,Y]=> opt.flatMap(p.lift) //This…

caeus
- 3,084
- 1
- 22
- 36
1
vote
2 answers
In a pattern matching partial function, how to make isDefined return false for invalid inputs that can't be included in the case pattern?
In a partial function implemented with pattern matching, how to make isDefined return false for invalid inputs that can't be included in the case pattern?
For example, I have the following decodeList partial function:
case class Arr(items:…

Shreck Ye
- 1,591
- 2
- 16
- 32
1
vote
3 answers
Why Scala PartialFunction works without defining isDefinedAt?
It looks First and Second are the same, but why?
First
val iter = List(1, 2, 3, 4, 5).iterator
val first = iter.collect(new PartialFunction[Int, Int]{
def apply(i: Int) = i
def isDefinedAt(i: Int) = i > 0 && i <…

mon
- 18,789
- 22
- 112
- 205
1
vote
0 answers
In Scala, Is it possible to have a "partially lazy" calculaltion of a dynamically calculated partial function?
With assistance from @Luis Miguel Mejía Suárez, I have a dynamically calculated partial function for changing arabic numbers to roman numerals:
import scala.collection.immutable.ListMap
import scala.collection.mutable.StringBuilder
object…

Brian Kessler
- 2,187
- 6
- 28
- 58
1
vote
1 answer
Type-safe states with exhaustive pattern matching in Akka Typed
I started working with Akka Typed to get exhaustive pattern matching on my behaviors, which has been great for the external facing contract of each actor. However, if the actor is a state machine, chances are the different states have state specific…

Arne Claassen
- 14,088
- 5
- 67
- 106
1
vote
3 answers
Scala function composition totalFn(partialFn(totalFn(x)))
I was trying to compose three functions with only the middle one being a PartialFunction. I would expect the resulting type to be PartialFunction as well.
Example:
val mod10: Int => Int = _ % 10
val inverse: PartialFunction[Int, Double] = { case n…
user2697852
1
vote
1 answer
Scala missing parameter type for expanded function The argument types of an anonymous function must be fully known. (SLS 8.5)
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…

HaaLeo
- 10,065
- 3
- 44
- 55
1
vote
2 answers
Scala type inference failing to infer types of a generic function
I have the following code
def sendMoney[T <: MoneyType](fn: T => Future[T], input: T): Unit
which is called this way
case x: Any => (sendMoney(_, _).tupled(x match {
case c: HoldsMoney => (createHold(_: HoldsMoney), c: HoldsMoney)
case r:…

ffff
- 2,853
- 1
- 25
- 44
1
vote
3 answers
Scala, default code for PartialFunction?
I am using Akka and would like to run some code for all cases of a PartialFunction. For actor supervision, I have something like:
val supervisorStrategy = OneForOneStrategy() {
case npe: NullPointerException => Stop
case re: RuntimeException =>…

ticofab
- 7,551
- 13
- 49
- 90