Questions tagged [partial-functions]

Partial functions are functions which are not defined for all of their inputs. For questions about partially applied functions, use [partial-application] instead.

35 questions
1
vote
1 answer

Factoring out common cases in pattern matching with partial function

I routinely use partial functions to factor out common clauses in exception handling. For example: val commonHandler: PartialFunction[Throwable, String] = { case ex: InvalidClassException => "ice" } val tried = try { throw new…
Igor Urisman
  • 717
  • 1
  • 6
  • 22
1
vote
1 answer

http4s route matching for GET requests with params on root host

I have simple rout-mapping function which using http4s: import cats.syntax.all._ import org.http4s._ import org.http4s.circe._ def routeMapper: PartialFunction[Request[F], F[Response[F]]] = { case r @ POST -> Root => create case r @ PUT -> Root…
Boris Azanov
  • 4,408
  • 1
  • 15
  • 28
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
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

PartialFunction implicit parameters

i have a simple PartialFunction type ChildMatch = PartialFunction[Option[ActorRef], Unit] def idMatch(msg: AnyRef, fail: AnyRef)(implicit ctx: ActorContext): ChildMatch = { case Some(ref) => ref forward msg case _ => ctx.sender() !…
HoTicE
  • 573
  • 2
  • 13
1
vote
1 answer

How to generate a stack trace upon division by zero with ghc 7.10.3?

If a program is run and crashes with the message "Divide by zero", what is the best way to identify where in the code this error was generated?
kostmo
  • 6,222
  • 4
  • 40
  • 51
1
vote
1 answer

Why does this function hang the REPL?

Chapter 9 of Test-Driven Development with Idris presents the following data type and removeElem function. import Data.Vect data MyElem : a -> Vect k a -> Type where MyHere : MyElem x (x :: xs) MyThere : (later : MyElem x xs) -> MyElem x (y…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
1
vote
1 answer

How to use Scala macros to create new partial functions or transform them?

I am having trouble writing a macro that transforms a given partial function and creates a new partial function. For instance, I want to be able to decompose the given partial function into its elements - pattern binder, guard condition, and body;…
winitzki
  • 3,179
  • 24
  • 32
1
vote
1 answer

Idris: function works with Nat parameter and fails type checking with Integer parameter

I am new to Idris. I am experimenting with types and my task is to make an "onion": a function that takes two arguments: a number and whatever and puts whatever into List nested such number of times. For example, the result for mkOnion 3 "Hello…
O.Phillips
  • 351
  • 1
  • 12
1
vote
2 answers

How to write Haskell partial functions that will never have invalid values?

Let's say want a function, taking a list of numbers, that returns the sum of adjacent pairs of numbers, except at the two edges where it simply returns the edge numbers. For example: [1,2,3,4,5] --> [1,3,5,7,9,5] I come up with the following…
user1002430
1
vote
1 answer

Scala partial function composition performance

Is there any difference in the performance of a partial function defined as val matches = { case Match(x,y) => ... case AnotherMatch(x,y,z) => ... case x:YetAnother => ... } and one defined as below? val match1 = { case Match(x,y) =>…
Cristian Vrabie
  • 3,972
  • 5
  • 30
  • 49
1
vote
5 answers

How to know when to use PartialFunction vs return Option

As an example, we define a function that should convert 1, 3, 42 respectively to "foo", "bar", "qix" and all other integer to "X". I've come up with 2 implementations : The method f need to be separate because it can be reuse in other context. def…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
0
votes
1 answer

WxPython PyPubSub, using curried function not working

I'm using the PyPubSub module of WxPython, to send messages around, and I want to have a function subscribed to a topic, where the function has some curried parameters. Unfortunately, it doesn't seem to be using the curried function as I would…
John C
  • 6,285
  • 12
  • 45
  • 69
0
votes
2 answers

PartialFunction in Scala

I am looking at the PartialFunction source code of Scala. In the file, the trait PartialFunction as well as a companion object PartialFunction are defined. The companion object has methods cond and condOpt. Link:…
user3103957
  • 636
  • 4
  • 16
0
votes
0 answers

partial functions vs input verification

I really love using total functions. That said, sometimes I'm not sure what the best approach is for guaranteeing that. Lets say that I'm writing a function similar to chunksOf from the split package, where I want to split up a list into sublists…
Nathan Wilson
  • 629
  • 6
  • 11