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
4
votes
2 answers
Scala Option's collect method doesn't like my PartialFunction
I think I'm missing something:
scala> Some(1) collect ({ case n if n > 0 => n + 1; case _ => 0})
res0: Option[Int] = Some(2)
scala> None collect ({ case n if n > 0 => n + 1; case _ => 0})
:6: error: value > is not a member of Nothing
…

pr1001
- 21,727
- 17
- 79
- 125
4
votes
3 answers
Anonymous PartialFunction syntax
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" }
…

Dima
- 39,570
- 6
- 44
- 70
4
votes
2 answers
How does isDefinedAt method work when case statements are used?
In this explanation of partial functions in scala, an case statement is used as follows:
val divide2: PartialFunction[Int, Int] = {
case d: Int if d != 0 => 42 / d
}
Then it says:
Although this code doesn’t explicitly implement the…

mahonya
- 9,247
- 7
- 39
- 68
4
votes
1 answer
How to avoid `missing type` error while chaining partial functions
I'm currently studying Scala & Akka and developing test application for it. In this application almost all actors log unhandled messages for ease of debug:
import akka.actor._
class TestActor extends Actor with ActorLogging {
def receive: Receive…

Seigert
- 311
- 3
- 11
3
votes
1 answer
Why does ($ 3) have signuature (a -> b) -> b?
In Learn you a Haskell, it is given the following example:
map ($ 3) [(4+), (10*), (^2), sqrt]
[7.0,30.0,9.0,1.7320508075688772]
However, I don't understand why this works.
The signatures of the functions are
Prelude> :info ($)
($) :: (a -> b) ->…

Our
- 986
- 12
- 22
3
votes
0 answers
How to perform a `Collectors.toList()` correspondingly in scala
I have the following class and method used in the code below with ids being a Set[String]:
case class PAV ()
def aggregate(pId: String, iId: Option[String], count: Int): PAV
I have tried the following:
One, using…

Naman
- 27,789
- 26
- 218
- 353
3
votes
2 answers
Why does providing a partial function to map throw at runtime rather than give a compile error?
Apologies if this is obvious but I am new to scala and I am getting two unexpected behaviors with the following code:
Seq(1, "a", 2, "b") map {
case i: Int => i+1
}
1) I would have expected to get back a collection where the strings are…

Stan L
- 33
- 5
3
votes
3 answers
Scala's notion of "partial functions"' & the ".orElse" method in F#
In Scala there's the notion of a "partial function" that is fairly similar to what F#'s function keyword allows me to achieve. However Scala's partial functions also allow for composition via the orElse method as shown below:
def intMatcher:…

shayan
- 1,211
- 9
- 12
3
votes
1 answer
Scala : Collect with generics
Given the following scenario
val items = List("a", "b", "c", 1, 2, 3, false, true)
def intItems = items.collect {case i : Int => i}
def stringItems = items.collect {case s : String => s}
is there a way to create a generic function to handle this…

Bruno
- 252
- 1
- 8
3
votes
2 answers
Combine a PartialFunction with a regular function
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…

Dima
- 39,570
- 6
- 44
- 70
3
votes
1 answer
MatchError for PartialFunctions composition with orElse
While writing a Specs2 specification for an Actor I got a somewhat puzzling MatchError for composition of several partial functions.
A minimal example:
val testPf1 = PartialFunction[Any, Boolean]{ case 2 ⇒ true }
val testPf2 = PartialFunction[Any,…

Sascha Kolberg
- 7,092
- 1
- 31
- 37
3
votes
2 answers
Why is this PartialFunction defined but still crashes (correctly) after application in Scala?
I would like to try partial functions with a deep pattern matching use case. This initially (of course) didn't work after applying Some(Some(3)), but seemed defined instead :
def deepTest : PartialFunction [Option[Option[Int]], Int] = {
case…

Aggelos Biboudis
- 1,175
- 8
- 27
3
votes
3 answers
Magic PartialFunction in Scala
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"
| }):…

Robin Green
- 32,079
- 16
- 104
- 187
2
votes
2 answers
Scala PartialFunction with isDefinedA and apply not working
I am new in Scala, i was trying PartialFunctions, is this correct way to test functionality, as some tutorials followed this to work, but not working for me?
code:
object MyScalaApp extends App {
def try29{
val r = new PartialFunction[Int, Int] …

Ashish Kamble
- 2,555
- 3
- 21
- 29
2
votes
1 answer
How can I use PartialFunctions to compose my match statements?
Consider the following:
scala> object Currency extends Enumeration {
| type Currency = Value
| val USD = Value
| val GBP = Value
| val EUR = Value
| val TRY = Value // Turkish lira
| val NGN = Value //…

pr1001
- 21,727
- 17
- 79
- 125