Matching on tuple using comparison operator - scala

I would like to match on tuple pattern, but I can not find any solution how to match using comparison operators. My code is:
myTuple match {
case (-1,-1,true) => ...
case (_>=0,-1,_) => ...
}
This gives give compile time error.
I also tried to use if guard, but as I see it can not be applied this way:
case (_ if _>=0,-1,_) => ...
Is my approach correct or should I solve this on an different way?
Thanks
Zoltan

The syntax is wrong, you should use guard as follows:
myTuple match {
case (-1,-1,true) => ...
case (x,-1,_) if x >= 0 => ...
case _ => ... // default
}
There are a lot of good introduction to scala pattern matching on the web. Here is the first detailed one, I've found on google: Playing with Scala's pattern matching

Related

Do something when exactly one option is non-empty

I want to compute something if exactly one of two options is non-empty. Obviously this could be done by a pattern match, but is there some better way?
(o1, o2) match {
case (Some(o), None) => Some(compute(o))
case (None, Some(o)) => Some(compute(o))
case _ => None
}
You could do something like this:
if (o1.isEmpty ^ o2.isEmpty)
List(o1,o2).flatMap(_.map(x=>Some(compute(x)))).head
else
None
But pattern matching is probably the better way to go.
Thanks to helpful comments from #Suma, I came up with another solutions in addition to the current ones:
Since the inputs are always in the form of Option(x):
Iterator(Seq(o1,o2).filter(_!=None))
.takeWhile(_.length==1)
.map( x => compute(x.head.get))
.toSeq.headOption
Using iterator also allows for a sequence of values to be passed to the input. The final mapping will be done if and only if one value in the sequence is defined.
Inspired by now deleted answer of pedrofurla, which was attempting to use o1 orElse o2 map { compute }, one possibility is to define xorElse, the rest is easy with it:
implicit class XorElse[T](o1: Option[T]) {
def xorElse[A >: T](o2: Option[A]): Option[A] = {
if (o1.isDefined != o2.isDefined) o1 orElse o2
else None
}
}
(o1 xorElse o2).map(compute)
Another possibility I have found is using a pattern match, but using Seq concatenation so that both cases are handled with the same code. The advantage of this approach is it can be extended to any number of options, it will always evaluate when there is exactly one:
o1.toSeq ++ o2 match {
case Seq(one) => Some(compute(one))
case _ => None
}
Just initialize a sequence and then flatten
Seq(o1, o2).flatten match {
case Seq(o) => Some(compute(o))
case _ => None
}

scala: convert match statement to pattern matching anonymous function - with values

like similar question: Convert match statement to partial function when foreach is used. Now similarly, IntelliJ asks me to improve my code. The difference is, that I use values for the matching:
val matchMe = "Foo"
keys.foreach(key =>
key match {
case `matchMe` => somethingSpecial()
case _ => somethingNormal(key, calcWith(key))
})
Refactoring this to a anonymous pattern-matching function would look something like:
keys.foreach {
case `matchMe` => somethingSpecial(_)
case _ => somethingNormal(_, calcWith(_)) //this doesn't work
}
Note that in the second case, I cannot use _ since I need it twice. Is there some way to use an anonymous pattern-matching function here?
You can't use the wildcard _ here, its purpose is to indicate you don't care about the value you're matching against.
You can use a named parameter :
keys.foreach {
case `matchMe` => somethingSpecial(matchMe)
case nonSpecialKey => somethingNormal(nonSpecialKey, calcWith(nonSpecialKey))
}
Without any restrictions placed on it, it will match any value. Do note that the order of cases is important, as case x => ... match anything and will essentially shortcut other case statements.
As an aside, I don't think your somethingSpecial(_) does what you want/expect it to. It's only a short version of x => somethingSpecial(x), not somethingSpecial(matchMe).

Pattern matching using current object

I'm trying to match an Option, and test to see if it's a Some containing the object making the call. So the code I want to write looks like this:
methodReturningOption() match {
case Some(this) => doSomething()
case _ => doSomethingElse()
}
but that fails to compile, with the error
'.' expected but ')' found
I also tried using Some(`this`) which gives the error
not found: value this
I can make it work if I add a variable which refers to this
val This = this
methodReturningOption() match {
case Some(This) => doSomething()
case _ => doSomethingElse()
}
but that looks ugly and seems like an unpleasant workaround. Is there an easier way to pattern match with this as an argument?
I suppose you could try this:
methodReturningOption() match {
case Some(x) if x == this => doSomething()
case _ => doSomethingElse()
}
It looks like this is considered a special keyword and can't be used in that context.
Jack Leow's solution is probably the best - I'd recommend going with that since it's much more explicit. However as an alternative you can also create a variable point to 'this' using the following syntax. (Note the self => on the first line)
class Person { self =>
def bla() = methodReturningOption() match {
case Some(`self`) => ???
case _ => ???
}
}
This doesn't really answer the question, it's just a potential alternative syntax that may be useful to you.

Counting with a matcher

Is there a shorter way to write the following (QuizWord is a case class):
list.count { case w: QuizWord => true; case _ => false}
Sometimes using isInstanceOf can be more readable than pattern matching. And this will be as efficient.
list.count(_.isInstanceOf[QuizWord])
Consider a similar pattern matching on the type using collect as follows,
list.collect { case w: QuizWord => w }.size

Scala guarded pattern with or matching

I would like to do a pattern match that looks like :
sinceOp match {
case None |Some(lastUpdate) if lastUpdate<= update.time =>
Saddly this does not work. Any ideas ?
Thanks
You could can also test the reverse condition:
sinceOp match {
case Some(lastUpdate) if lastUpdate > update.time => //...
case _ => //...
}
The second case covers both None and the case where the last update is smaller.
Or you can replace pattern matching with chain of functions
sinceOp.filterNot(_ <= update.time).getOrElse(println("if None"))