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
6
votes
2 answers

Scala: selecting function returning Option versus PartialFunction

I'm a relative Scala beginner and would like some advice on how to proceed on an implementation that seems like it can be done either with a function returning Option or with PartialFunction. I've read all the related posts I could find (see bottom…
Gregor Scheidt
  • 3,952
  • 4
  • 24
  • 28
6
votes
2 answers

In functional programming terms, what do you call something with an orElse or other fallback method?

Using scala for reference, we see a fallback behavior (orElse) in several places such as PartialFunction, Option, and cats EitherOps. This feels similar to but not the same as the flattening behavior of a monad. Is there a functional programming…
kag0
  • 5,624
  • 7
  • 34
  • 67
6
votes
2 answers

Scala Partial Function Type Definition

val even: PartialFunction[Int, String] = PartialFunction[Int, String] { case i if i % 2 == 0 => i + " is even" } val isEven: PartialFunction[Int, String] = { case i if i % 2 == 0 => i + " is even" } val odd: PartialFunction[Int, String] =…
pramesh
  • 1,914
  • 1
  • 19
  • 30
6
votes
2 answers

Is there a nicer way of lifting a PartialFunction in Scala?

I occasionally come across the following pattern, where I essentially have a PartialFunction[SomeType,AnotherType], and want to treat it as a Function[SomeType,Option[AnotherType], eg: def f(s:SomeType):Option[AnotherType] = s match { case…
Kristian Domagala
  • 3,686
  • 20
  • 22
6
votes
3 answers

convert function to partial function scala

I have a sealed trait: sealed trait ActorMessage case class AddX(x: Int) extends ActorMessage case class RemoveX(x: Int) extends ActorMessage Also I have a function to handle all messages and warn me about non exhaustive match: def handleMessage:…
kpbochenek
  • 469
  • 2
  • 21
6
votes
3 answers

Using Tuples in map, flatmap,... partial functions

If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { t => t._1 + t._2 } It's ok. If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { case (b, n) => b + n } It's ok too. But if I do: val l…
Joan
  • 4,079
  • 2
  • 28
  • 37
6
votes
2 answers

Extending a partially implemented partial function in scala

I'm using the Akka actors library here. The actors library defines a partial function "receive" which an actor that extends "actor" must implement to deal with various messages. I am creating a trait hierarchy for my application where trait…
Alex
  • 650
  • 9
  • 23
5
votes
2 answers

Scala PartialFunctions from concrete ones

Is there any quick way to use as a concrete function (of type, say, (A) => B) as a PartialFunction[A, B]? The most concise syntax I know of is: (a: A) => a match { case obj => func(obj) } Is there an implicit conversion anywhere, something…
Kenneth Allen
  • 347
  • 3
  • 7
5
votes
3 answers

Understand 'case' keyword in partial functions

I am new to Scala and I am trying to decode its constructs, I learned about pattern matching and the syntax is similar to Java switch statement val x: Int = Random.nextInt(10) x match { case 0 => "zero" case 1 => "one" case 2 => "two" case…
Adelin
  • 18,144
  • 26
  • 115
  • 175
5
votes
2 answers

Scala: Is it possible to get partially applied function from leftfold?

I'm currently learning Scala, and I just wondered at fold-left. Since fold-left is curried, you should be able to get a partially applied function(PAF) with a first parameter as below. (0 /: List(1, 2, 3)) _ But actually, I've got an…
Tamajin
  • 155
  • 5
5
votes
4 answers

Is PartialFunction orElse looser on its type bounds than it should be?

Let's define a PartialFunction[String, String] and a PartialFunction[Any, String] Now, given the definition of orElse def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]): PartialFunction[A1, B1] I would expect not to be able to compose the…
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
5
votes
2 answers

Define an object extending PartialFunction, implement directly with cases

I'm quite new to Scala but I already love it. I have read tutorials and articles on partial functions. What I would like to achieve is to have an object extending PartialFunction[...,...] and have it defined directly with cases, without needing to…
azyoot
  • 1,162
  • 8
  • 18
5
votes
1 answer

Scala value class compilation fails for base type with partial-function-parameter method

Say, I defined a value class as follows package object p { class ValueClass[T](val o: Option[T]) extends AnyVal { def foo: Option[T] = o collect { case t => t } } } The compilation failed with the message: overriding…
5
votes
2 answers

PartialFunction and MatchError

There are two ways to define PF: 1) with literal case {} syntax and 2) as explicit class. I need the following function throw a MatchError, but in the second case that doesn't happen. 1) with case val test: PartialFunction[Int, String] = { case x…
user1078671
5
votes
1 answer

Scala: Have the type parameter of a collection survive a "collect" when the type parameter is a member type

Normally, when collecting all the elements of a sequence that match a particular type, the resulting collection has both the type of the original collection and the type selected for: trait Foo trait Bar trait Baz { // Works def def1(foo:…
drhagen
  • 8,331
  • 8
  • 53
  • 82