Partial functions are functions which are not defined for all of their inputs. For questions about partially applied functions, use [partial-application] instead.
Questions tagged [partial-functions]
31 questions
66
votes
3 answers
What exactly is meant by "partial function" in functional programming?
According to my understanding, partial functions are functions that we get by passing fewer parameters to a function than expected. For example, if this were directly valid in Python:
>>> def add(x,y):
... return x+y
...
>>> new_function =…
![](../../users/profiles/5105791.webp)
Saurabh kukade
- 1,364
- 8
- 19
31
votes
2 answers
In Haskell, why non-exhaustive patterns are not compile-time errors?
This is a follow-up of Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function?
I understand that using -Wall, GHC can warn against non-exhaustive patterns. I'm wondering what's the reason behind not…
![](../../users/profiles/138830.webp)
gawi
- 13,050
- 7
- 38
- 74
10
votes
2 answers
Why is take a total function
take (-1) [] is [].
What are the reasons to prefer this over a partial function, that is, an error?
Are there use cases where this property is exploited?
![](../../users/profiles/772868.webp)
false
- 10,182
- 12
- 93
- 182
9
votes
5 answers
Scala total function as partial function
Since a total function is a special case of a partial function, I think I should be able to return a function when I need a partial.
Eg,
def partial : PartialFunction[Any,Any] = any => any
Of course this syntax fails to compile. My question is, is…
![](../../users/profiles/2048448.webp)
sksamuel
- 15,025
- 8
- 54
- 97
6
votes
1 answer
How does Scala Cons pattern matching determine the head and the tail of a List?
How is the head and tail determined in the following statement:
val head::tail = List(1,2,3,4);
//head: 1 tail: List(2,3,4)
Shouldn't there be some piece of code which extracts the first element as head and returns the tail as a new List. I've…
![](../../users/profiles/4060251.webp)
poissondist
- 83
- 4
5
votes
1 answer
Is scanl1 really partial?
According to the Haskell wiki, the scanl1 function is partial. I don't understand what inputs result in bottom, though. For list functions, I'm used to the problem inputs either being empty lists (like for head) or infinite ones (like for reverse).…
![](../../users/profiles/7509065.webp)
Joseph Sible-Reinstate Monica
- 34,270
- 3
- 35
- 75
5
votes
2 answers
Scala's Partial Functions in Haskell
Scala has a very nice support of partial functions, mainly because in Scala when you define a partial function it also defines an isDefinedAt function for it. And also Scala has orElse and andThen functions to work with partial functions.
Haskell…
![](../../users/profiles/3315928.webp)
Gurmeet Singh
- 315
- 1
- 5
5
votes
5 answers
Short circuiting a list mapping with a partial function
So, I have made this function called tryMap which is as follows:
/// tryMap, with failure and success continuations.
let rec tryMapC : 'R -> ('U list -> 'R) -> ('T -> 'U option) -> ('T list) -> 'R =
fun failure success mapping list ->
…
![](../../users/profiles/1738495.webp)
phaz
- 852
- 8
- 23
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" }
…
![](../../users/profiles/4254517.webp)
Dima
- 33,157
- 5
- 34
- 54
2
votes
1 answer
How to getOrElse with another Option in Scala
Let's assume that we have an option foo1 and an option foo2:
val foo1: Option[Foo]
val foo2: Option[Foo]
Is there an operator/function that allows me to return the value of the foo2 when foo1 is None?
val finalFoo: Option[Foo] =…
![](../../users/profiles/1035008.webp)
Yuchen
- 24,092
- 18
- 133
- 193
2
votes
2 answers
Partial function explanation in the Odersky book
In the Scala Odersky book, he has an example explaining partial functions of page 295. It starts with this function:
val second: List[Int] => Int = {
case x :: y :: _ => y
}
So the above function will succeed if you pass it a three element list…
![](../../users/profiles/2714301.webp)
Jwan622
- 8,910
- 11
- 56
- 125
2
votes
0 answers
Anonymous partial function in early initializer requires "premature access to class"
Why does this fail to compile:
trait Item
trait StringItem extends Item {
def makeString: String
}
trait SomeOtherItem extends Item
trait DummyTrait
case class Marquee(items: Seq[Item]) extends {
val strings: Seq[String] = items.collect {
…
![](../../users/profiles/1393162.webp)
Ben Kovitz
- 4,279
- 1
- 17
- 40
2
votes
1 answer
Applying partial functions where defined and a different function where not
This is a motivational example, Given:
List((1,2), (2,1), (3,1))
I'd like to return:
List((1,2),(3,1))
I've tried to do this in several ways. First:
List((1,2), (2,1), (3,1)) map { case (a,b) => if (a > b) (a,b) else (b,a) }
distinct
Then I…
![](../../users/profiles/708909.webp)
Joselo
- 93
- 1
- 7
2
votes
1 answer
Explanation of List[_] in pattern matching used in flatten function
I am new to scala and I can not understand the following function
val L = List(List(1, 1), 2, List(3, List(5, 8)))
def flatten(l: List[Any]): List[Any] = l flatMap {
case ms:List[_] => flatten(ms)
case l => List(l)
} …
![](../../users/profiles/2411173.webp)
Donbeo
- 14,217
- 30
- 93
- 162
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…
![](../../users/profiles/6770614.webp)
Boris Azanov
- 3,167
- 1
- 8
- 20