When you pattern match again Lists, you can use Nil to check for empty list. However, if the underlying type is an Iterable, you can still check for Nil, and it will break for empty Sets, etc... See following REPL session:
scala> val l: Iterable[Int] = List()
l: Iterable[Int] = List()
scala> l match {
| case Nil => 1
| case _ => 2
| }
res0: Int = 1
scala> val l: Iterable[Int] = Set()
l: Iterable[Int] = Set()
scala> l match {
| case Nil => 1
| case _ => 2
| }
res2: Int = 2
Question is - how can I prevent this kind of issue? Obviously, if l is a type List, it's no bug. And if l is of type Set, it won't compile. But what if we have a class that has a list, define a function that pattern matches in this way, and then someone changes the class to take a generic iterable instead? Is this Nil vs. _ pattern match a bad idea in general?
One possibility is to use a guard:
scala> val xs: Iterable[Int] = Set()
xs: Iterable[Int] = Set()
scala> xs match { case xs if xs.isEmpty => 1 case _ => 2 }
res0: Int = 1
Another way to do this is to use an if-else-expression (works best if you only have one or two conditions to check):
scala> if (xs.isEmpty) 1 else 2
res1: Int = 1
Convert the scrutinee to a list to eliminate doubt.
l.toList match {
case Nil => 1
case xs => 2
}
Here is another option (pun intended):
scala> val l: Iterable[Int] = List()
l: Iterable[Int] = List()
scala> l.headOption match { case None => 1; case Some(h) => 2 }
res0: Int = 1
This is useful in cases where you pattern match in order to get the head like in popular List() match { case h :: t => ... } but it's not a list, it's an Iterable and :: will fail.
I added this answer because I thought it's quite common to pattern match on a collection to get a head, otherwise you can just check with xs.isEmpty.
Related
Consider a flatMap written over some case matching. For example:
list.flatMap( v =>
v match {
case Cond1 => if(something) Some(Int) else None
//..Other conditions yielding Option[Int]
case CondN => if(somethingelse) Seq(Int) else Seq()
})
However this wont compile. If the seq is all of Option[Int] or all of Seq[Int] the flatMap works. But not if the Seq is a mix of Options and Seqs. Why is such a restriction in place? Does this solve a particular ambiguity that I cannot think of as of now.
EDIT1
Adding code snippet from REPL
scala> val a = Seq(Option(1), Seq(2,3))
a: Seq[Equals] = List(Some(1), List(2, 3))
scala> val b = Seq(Seq(1), Seq(2,3))
b: Seq[Seq[Int]] = List(List(1), List(2, 3))
scala> a.flatMap(x=>x)
<console>:9: error: type mismatch;
found : Equals
required: scala.collection.GenTraversableOnce[?]
a.flatMap(x=>x)
^
scala> b.flatMap(x=>x)
res24: Seq[Int] = List(1, 2, 3)
EDIT2
After Filippo's answer I tried the following piece of code in the REPL and it worked.
scala> val options = Seq("opt1", "opt2")
options: Seq[String] = List(opt1, opt2)
scala> options.flatMap( x =>
| x match {
| case "opt1" => Some(1)
| case "opt2" => Seq(2,3)
| case _ => None
| })
res27: Seq[Int] = List(1, 2, 3)
How is the resolution different in each of the scenarios.
More importantly when I map instead of flatMap the result is the same as the Seq a that I had created.
scala> options.map( x =>
| x match {
| case "opt1" => Some(1)
| case "opt2" => Seq(2,3)
| case _ => None
| })
res28: Seq[Equals] = List(Some(1), List(2, 3))
Option is a GenTraversableOnce but scala needs some help here:
val a: Seq[TraversableOnce[Int]] = Seq(Option(1), Seq(2,3))
a.flatMap(x=>x)
res0: Seq[Int] = List(1, 2, 3)
EDIT after additions to the question
I think that if the type of your sequence is the one you are expecting, everything boils down to the function passed to the flatMap. If scala can't figure out that the function is (A) => Traversable[A] when the starting sequence is Seq[A], I think we should make some types explicit.
Now, back to your first sample, I would refactor it as:
list.flatMap {
case Cond1 if something => Seq(Int)
case CondN if somethingelse => Seq(Int)
case _ => Seq()
}
No doubt scala is now able to infer the types correctly.
I start learning Scala and I don't quite understand some behaviors of pattern-matching. Can anyone explain to me why the first case works but the second case doesn't work?
1
def getFirstElement(list: List[Int]) : Int = list match {
case h::tail => h
case _ => -1
}
Scala> getFirstElement(List(1,2,3,4))
res: Int = 1
Scala> 1 :: List(1,2)
res: List[Int] = List(1, 1, 2)
2
def getSumofFirstTwoElement(list: List[Int]): Int = list match {
case List(a: Int, b: Int)++tail => a + b
case _ => -1
}
<console>:11: error: not found: value ++
Scala> List(1,2) ++ List(3,4,5)
res: List[Int] = List(1, 2, 3, 4, 5)
The reason is that there is no unapply method on an object of type ++. The reason that :: works is because behind the scenes it is really:
final case class ::[B](override val head: B, private[scala] var tl: List[B]) extends List[B] {
override def tail : List[B] = tl
override def isEmpty: Boolean = false
}
Source
This leads to how pattern matching is implemented in Scala. It uses extractors (or here), which are basically objects that contain an unapply method, which is provided by default with case classes.
++ is a method on object of list. I think you want:
def getSumofFirstTwoElement(list: List[Int]): Int = list match {
case a::b::tail => a + b
case _ => -1
}
In case two, ++ isn't used for unapply. You want the extractor :: to decompose the list.
A good explanation here.
scala> def getSumofFirstTwoElement(list: List[Int]): Int = list match {
| case a::b::t => a + b
| case _ => -1
| }
getSumofFirstTwoElement: (list: List[Int])Int
scala> getSumofFirstTwoElement(List(1,2,3,4))
res0: Int = 3
When pattern matching a list, it seems common to return an empty list when given an empty list. We can match an empty list to Nil or List(), but we can return empty as Nil, List() or by returning the given list argument itself.
What's the convention here?
When would you choose one method over another?
Examples:
def givenEmptyNumsReturnsNil(nums: List[Int]): List[Int] = nums match {
case List() => Nil
case x :: xs => ???
}
def givenEmptyNumsReturnsEmptyList(nums: List[Int]): List[Int] = nums match {
case List() => List()
case x :: xs => ???
}
def givenEmptyNumsReturnsNums(nums: List[Int]): List[Int] = nums match {
case List() => nums
case x :: xs => ???
}
I'm scala beginner and don't know any existing convention about it. My things about it:
The last one isn't intuitive
I prefer return that I match. If I have case List(), so I return List().
You can also match Nil:
-
def givenEmptyNumsReturnsNil(nums: List[Int]): List[Int] = nums match {
case Nil => Nil
case x :: xs => ???
}
But Nil and List() are the same.
For choice better way, just clarify what inside:
case N1:
def givenEmptyNumsReturnsEmptyList(nums: List[Int]): List[Int] = nums match {
case List() => List()
case x :: xs => ???
}
Will call unaplay method from object List, after will call apply method of object List.
case N2:
def givenEmptyNumsReturnsNil(nums: List[Int]): List[Int] = nums match {
case Nil => Nil
case x :: xs => ???
}
Will compare value before match with object Nil and will return object Nil
And in case of choice I prefer case N2 because it is little bit optimal.
I am trying to match the case where a Seq contains Nothing.
models.Tasks.myTasks(idUser.toInt) match {
case tasks => tasks.map {
task => /* code here */
}
case _ => "" //matches Seq(models.Tasks)
}
How is Seq[Nothing] represented in pattern matching ?
Matching against an empty sequence looks like this:
val x: Seq[Nothing] = Vector()
x match {
case Seq() => println("empty sequence")
}
EDIT: Note that this is more general than case Nil since Nil is a subclass only of List, not Seq in general. Strangely, the compiler is ok with matching against Nil if the type is explicitly annotated as Seq, but it will complain if the type is any non-List subclass of Seq. Thus you can do this:
(Vector(): Seq[Int]) match { case Nil => "match" case _ => "no" }
but not this (fails with compile-time error):
Vector() match { case Nil => "match" case _ => "no" }
Assuming I understand what you mean correctly, a sequence that contains nothing is empty, which is Nil:
case Nil => //do thing for empty seq
This works even though you're dealing with Seqs, not Lists:
scala> Seq()
res0: Seq[Nothing] = List()
scala> Seq() == Nil
res1: Boolean = true
Some more REPL output to show that this works absolutely fine with other subclasses of Seq:
scala> Nil
res3: scala.collection.immutable.Nil.type = List()
scala> val x: Seq[Int] = Vector()
x: Seq[Int] = Vector()
scala> x == Nil
res4: Boolean = true
scala> x match { case Nil => "it's nil" }
res5: java.lang.String = it's nil
scala> val x: Seq[Int] = Vector(1)
x: Seq[Int] = Vector(1)
scala> x match { case Nil => "it's nil"; case _ => "it's not nil" }
res6: java.lang.String = it's not nil
As can be seen from the above output, Nil is a type all of it's own. This question has some interesting things to say on the matter.
But #dhg is correct that if you manually create a specific subtype such as vector, the match does not work:
scala> val x = Vector()
x: scala.collection.immutable.Vector[Nothing] = Vector()
scala> x match { case Nil => "yes"}
<console>:9: error: pattern type is incompatible with expected type;
found : object Nil
required: scala.collection.immutable.Vector[Nothing]
x match { case Nil => "yes"}
Having said that, I don't know why you would need to force your objects to be a referred to as a specific concrete subclass very often.
Given the following type and instance:
type operation = (Int, Int) => Int
def add: operation = _ + _
If I try to match an operation in a case statement, Scala complains about unchecked typing due to type erasure:
for (a <- elements) a match {
case o: operation => // do stuff
}
Is there a way to achieve this kind of function-based typing while being erasure-friendly in case statements?
Note, this is similar to this thread.
One easy way to deal with type erasure is to create an unparamaterized class. It's not perfect, but it works. Make it a case class that extends Function2 and it's not even too clunky to use either directly or in a pattern match
scala> case class Operation(f : (Int,Int) => Int) extends ((Int,Int) => Int) {
| def apply(x : Int, y : Int) = f(x,y)
| }
defined class Operation
scala> def add = Operation(_ + _)
add: Operation
scala> val y = add(7,3)
y: Int = 10
scala> val elements = List(1, add, 2)
elements: List[Any] = List(1, <function2>, 2)
scala> for (a <- elements) yield a match {
| case Operation(f) => f(1,2)
| case x : Int => x
| }
res0: List[Int] = List(1, 3, 2)
The limitation is that you have to have "boxed" the operation before you lose its type, not after. Also, you end up with one class per concrete function type.
Another, arguably much better, solution is to not lose the type information. Use an Either to retain the static type info.
scala> val elements : List[Either[Int, (Int, Int) => Int]] = List(Left(1), Right(_ + _), Left(2))
elements: List[Either[Int,(Int, Int) => Int]] = List(Left(1), Right(<function2>), Left(2))
scala> for (a <- elements) yield a match {
| case Right(f) => f(1,2)
| case Left(x) => x
| }
res1: List[Int] = List(1, 3, 2)
The limitation here is that it gets clunky if your List can have more than 2 types. But it effectively avoids forcing Scala to be a dynamically typed language, unlike the previous solution.
If you can wrap a into an Option, then this will work:
scala> val a:Option[Any] = Some(add)
a: Option[Any] = Some(<function2>)
scala> a match { case o:Some[operation] => println ("found"); case _ => }
found