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
0
votes
1 answer

Why does implicit conversion not work with PartialFunction

Say I define the following: type Func1 = PartialFunction[Int, String] case class A(f: Int => String) implicit def toA(func: Func1): A = A(func(_)) Then I might want to use the implicit conversion thus: val a: A = { case i: Int =>…
user79074
  • 4,937
  • 5
  • 29
  • 57
0
votes
1 answer

Scala: lift on Array

So I have an array and I can do: myArr.lift(0) ...and it gives me option of the value at index 0. So what actually is happening here? When I try to go to lift definition, IDE takes me to PartialFunction, and I see Array doesn't inherit from it. And…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
0
votes
2 answers

How to compose partial functions with multiple parameters in Scala

val f1 = (x: Int) => x match { case x1 => x * 2 } val f2 = (x: Int, y: Int) => (x, y) match { case (x1, y1) => x1 + y1 } val f3 = f1.compose(f2) Expecting f3 to be a partial function from (Int, Int) => Int…
0
votes
1 answer

collectFirst: Applying partial function with multiple case clauses

So I have defined a partial function to be used for collectFirst method in collection: myList.collectFirst{ case A(_,_,_) => .... case B(_,_,_) => .... case C(_,_,_) => .... } If myList contains A,B,C all then which case would be executed?
Mandroid
  • 6,200
  • 12
  • 64
  • 134
0
votes
3 answers

What is the difference between anonymous functions and partial functions?

I was reading about scala anonymous functions here and saw that they can take the format: { case p1 => b1 … case pn => bn } However, I thought that this was how partial functions were written. In fact, in this blog post, the author calls a partial…
RyanQuey
  • 705
  • 1
  • 9
  • 29
0
votes
1 answer

Why does Scala PartialFunction isDefinedAt method always return true?

my code is as below. case class C[T]() { val pf:PartialFunction[Any,Any] = { case i:T => i } } println(C[Int]().pf.isDefinedAt(-1.0)) this prints true. Why this happens?
0
votes
1 answer

In Scala, Is it possible to build up a collection of partial functions into a function by iterating over a ListMap?

Yesterday, @Krzysztof Atłasik helped me figure out how to reduce redundancy in matching by using partial functions, so what used to look like: i match { case x if x == 0 ⇒ romanNumeral case x if x >= 1000 ⇒ …
Brian Kessler
  • 2,187
  • 6
  • 28
  • 58
0
votes
3 answers

Is it possible to decompose Scala match statements using partial functions?

I have a match statement like this: i match { case x if x == 0 ⇒ romanNumeral case x if x >= 1000 ⇒ this.roman(i - 1000, s"${romanNumeral}M") case x if x >= 900 ⇒ this.roman(i - 900,…
0
votes
2 answers

Scala Regex Partial Function with Regex defined in Partial Function

Question From this answer to a related Scala Regex Partial Function question, I am able to define a partial function which matches a regex pattern using the following code: val regex = "(\\d+),([A-z]+)".r val input = List("1,a", "23,zZ", "1", "1ab",…
krismath
  • 1,879
  • 2
  • 23
  • 41
0
votes
2 answers

Composition of partial functions to reduce code length

I want to compose a functions in a specific way to reduce code length For instance composing a simple add function as such : val add3 = (a: Int, b:Int) => (c:Int) => a+b+c val composed = (a:Int, b:Int) => n:add3(a, b) => n(1) + n(2) +…
nbreizh
  • 33
  • 3
0
votes
1 answer

How to simplify method using Option or PartialFunction

I need some help to modify(simplify) my code. Here an example: def getBids(rawBids: RDD[List[String]], exchangeRates: Map[String, Double]): RDD[BidItem] = { rawBids.filter(bidList => !bidList(2).matches(ERROR_REGEXP)) .flatMap(bidList =>…
Pavel Orlov
  • 23
  • 2
  • 6
0
votes
1 answer

Missing parameter type in applyOrElse Scala

I have this code where I'm trying to call a partial function. When I build my project I get an error stating missing parameter type ++ headerExtractor.applyOrElse(event, _ => Map.empty). I've looked at other posts, but I feel like this should be…
Rafa
  • 3,219
  • 4
  • 38
  • 70
0
votes
2 answers

Composing partial functions in Scala

I have two partial functions f1 and f2 which I want to compose into a new partial function f so that f.isDefinedAt(x) iff f1.isDefinedAt(x) || f2.isDefinedAt(x). I mean -- | f1(x) iff f1.isDefinedAt(x) | f(x) = | | …
St.Antario
  • 26,175
  • 41
  • 130
  • 318
0
votes
3 answers

Order of evaluation of cases in Scala partial function

Can I assume an order on the evaluation of cases of a partial function in Scala? So for instance, given protected val eval: PartialFunction[(TestPrimitive, Seq[Literal]), Boolean] = { case (L3IntLt, Seq(IntLit(x), IntLit(y))) => x < y case…
user1868607
  • 2,558
  • 1
  • 17
  • 38
0
votes
1 answer

Pattern-matching: split code with PartialFunction?

I'm struggling to achieve a "simple" behaviour: divide pattern matching code in two separate functions. I'm simplifying the model for clearness purpose: abstract class Animal case object Dog extends Animal case object Cat extends Animal case object…