Is it possible to rewrite the following code
for (i <- x) {
if (i==x.first) {
// do sth
} else if (i==x.last) {
// do sth
} else {
// do sth
}
}
using pattern matching like
for (i <- x) i match {
case `x.first` => // do sth
case `x.last` => // do sth
case _ => // do sth
}
I know we can use guard, or evaluate x.first and x.last in advance and store them in other vals to quote here, but that’s just ugly. Any ideas? Thanks!
One clean way to do it would be to define extractors
+:and:+for yourself:Then you can simply do:
Which prints: