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
1
vote
1 answer
Partial Function Application in Scala
I'm learning Functional Programming, by following the book Functional Programming in Scala by Paul Chiusano and Rúnar Bjarnason. I'm specifically on chapter 3, where I am implementing some companion functions to a class representing a singly-linked…

Bruno Oliveira
- 735
- 10
- 20
1
vote
1 answer
Adapter for a PartialFunction
I am trying to come up with a combinator that would allow me do something like this:
def pfAdapter(pf: PartialFunction[String, String]): PartialFunction[(String,String), String]) = {
case (a,b) if(pf.isDefinedAt(a)) => pf(a)
}
Basically, I…

Dima
- 39,570
- 6
- 44
- 70
1
vote
2 answers
applying partial function on a tuple field, maintaining the tuple structure
I have a PartialFunction[String,String] and a Map[String,String].
I want to apply the partial functions on the map values and collect the entries for which it was applicaple.
i.e. given:
val m = Map( "a"->"1", "b"->"2" )
val pf :…

Eyal Farago
- 63
- 7
1
vote
1 answer
Passing an implicit parameter to Future.recover
I want to pass an implicit parameter to a partial function I use to recover my Futures.
def delete(id: Long) = ... { implicit something =>
serviceLayer.doSomething(id).recover(errorHandler)
}
def errorHandler: PartialFunction[Throwable, Result] =…

Nader Ghanbari
- 4,120
- 1
- 23
- 26
1
vote
1 answer
Scala applying a PartialFunction with () is not the same as .apply()
I'm trying to refactor my scala code in a project (Play Framework 2.4) when I came with this idea:
(To provide a minimal working example, I've changed some classes, for instance, I have changed Result and Future[Result] with Int and Option[Int]…

vicaba
- 2,836
- 1
- 27
- 45
1
vote
1 answer
How to call function overloaded by function which return partail function
How to print "I'm not partial" using one of below f functions, and why below code is printing "I'm partial"? And maybe there are some general rules connected with functions (maybe with arity 0) which produced partial functions and name overloading…

panurg
- 309
- 4
- 11
1
vote
2 answers
Is this scalac bug?
class X[A](val value: A){
def unapply[B <: A](x: X[B]) = true
}
object Main extends App {
val int = new X(1)
val string = new X("a")
val pf: PartialFunction[Any, Int] = { case o @ int() => o.value }
println(pf(string) +…

Kenji Yoshida
- 3,108
- 24
- 39
1
vote
1 answer
Is it possible to cancel current partial function from function body?
I need to define some partial functions to handle different urls with different patterns (with unfinagled):
def indexRoute:PartialFunction[Request, Response] = {
case Path("index") => Ok ~> ResponseString("hello")
}
def…

Freewind
- 193,756
- 157
- 432
- 708
1
vote
2 answers
Implementing a scala method using a partial function
I have a class, as follows:
trait Foo {
def greet(name: String) : String
}
trait Bar {
def hello(name: String) = s"Hello ${name}!"
}
class Greeter extends Foo with Bar {
def greet(name: String) = hello(name)
}
I'm curious if it is possible…

Chris Leishman
- 1,777
- 13
- 19
1
vote
1 answer
Checking if a partial function in scala is definied for a value with unknow type
I have the following trait (to get kind of rank 2 polymorphism click)
type Id[A] = A
trait ~>[F[_], G[_]] {
def apply[A](a: F[A]): G[A]
def isDefinedAt[A](a: A): Boolean}
And a function to transform a partial function to this trait:
implicit def…

schlicht
- 4,735
- 1
- 13
- 23
1
vote
1 answer
Weird error while using resetLocalAttrs
I have coded a macro which uses the resetLocalAttrs method of the (macro) context. After the macro has expanded, I get a weird error and I can't figure out what is happening. Firstly, I will introduce the problem. We have two primitives: the state…

neutropolis
- 1,884
- 15
- 34
0
votes
2 answers
Extract the main value from partial case
Let's say I'm defining some cases to match, where I care only about verifying an argument:
BytecodeChains.partial {
case CallProperty(name, args) if name == someConstant => xxx
} ....
where the function signature is:
BytecodeChains.partial[A]…

viraptor
- 33,322
- 10
- 107
- 191
0
votes
2 answers
How to define a more concise scala function
I am using the akka library and supplying a partial function to be implemented by an actor at runtime via a hot swap.
The akka hot swap takes an argument in the form PartialFunction[Any, Unit]. I have defined mine as the following:
class…

Sean W
- 11
- 2
0
votes
1 answer
My subprogram does not work in C language
I wrote a subprogram in C with Xcode that calculates a partial function but it does not work.
I know there is a a lot wrong with " return " statements but i couldn't find a way to make it better.
I made "return (result * 1 )" and "return(result =…

plathys
- 9
- 2
0
votes
1 answer
In Scala 2, what are possible ways to write a shortcut of a partial function without triggering unchecked warning?
This is a follow-up question of:
In Scala 3: Why runtime pattern matching can't work reliably on duck type using JVM reflection?
I'm trying to create a pattern matching implementation in Scala that is more resilient to type erasure. It should still…

tribbloid
- 4,026
- 14
- 64
- 103