All Questions

Filter by
Sorted by
Tagged with
12 votes
2 answers
1k views

Scala extractors - skip unused parameters

given following code: abstract class MyTuple ... case class MySeptet(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int, g: Int) extends MyTuple case class MyOctet(a: Int, b: Int, c: Int, d: Int, e: ...
vucalur's user avatar
  • 6,057
10 votes
6 answers
14k views

Scala: Pattern matching when one of two items meets some condition

I'm often writing code that compares two objects and produces a value based on whether they are the same, or different, based on how they are different. So I might write: val result = (v1,v2) match {...
waterlooalex's user avatar
  • 13.8k
9 votes
1 answer
3k views

Understanding pattern matching on lists

I've been playing around with Extractors lately and was wondering how the List extractors work especially this: List(1, 2, 3) match { case x :: y :: z :: Nil => x + y + z // case ::(x, ::(y, ::(...
raichoo's user avatar
  • 2,557
9 votes
3 answers
8k views

Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala

I have a simple class hierarchy that represents a graph-like structure with several distinct types of vertexes implemented using case classes: sealed trait Node sealed abstract case class Vertex ...
Ivan Poliakov's user avatar
8 votes
5 answers
4k views

Scala, partial functions

Is there any way to create a PartialFunction except through the case statement? I'm curious, because I'd like to express the following (scala pseudo ahead!)... val bi = BigInt(_) if (bi.isValidInt) ...
aioobe's user avatar
  • 417k
8 votes
1 answer
6k views

Pattern matching against Scala Map entries

Is there any Scala trick to enable pattern matching against map keys? In other words, I'd like to have an extractor that beside the Map instance accepted also a key value that would mean I want this ...
Jiří Vypědřík's user avatar
7 votes
1 answer
2k views

Nested Scala matchers why Some(Some(1),1) can't match?

It seems that nested matching doesn't work, which is a strange limitation. An example of the behaviour follows: Some(Some(1),2) match { | case Some(Some(a),b) => a | case e => e | } <...
Bryan Hunt's user avatar
  • 3,743
6 votes
3 answers
2k views

Modeling with Scala case class

I'm attempting to model responses from REST APIs as case classes which I can use pattern matching on. I thought it would be a good fit assuming inheritance, but I see that this is deprecated. I know ...
7zark7's user avatar
  • 10k
4 votes
2 answers
2k views

unapply method of a case class is not used by the Scala compiler to do pattern matching, why is that?

abstract class Animal case class Cat(name: String) extends Animal case class Dog(name: String) extends Animal Say I have defined Cat and Dog, two case classes. Then I use them like this: val ...
Cui Pengfei 崔鹏飞's user avatar
4 votes
2 answers
2k views

Implicit parameters won't work on unapply. How to hide ubiquitous parameters from extractors?

Apparently unapply/unapplySeq in extractor objects do not support implicit parameters. Assuming here an interesting parameter a, and a disturbingly ubiquitous parameter b that would be nice to hide ...
ib84's user avatar
  • 675
4 votes
1 answer
267 views

Scala 2.8.0.RC2 compiler issue on pattern matching statement?

Why does the following module not compile on Scala 2.8.RC[1,2]? object Test { import util.matching.Regex._ val pVoid = """\s*void\s*""".r val pVoidPtr = """\s*(const\s+)?void\s*\*\s*""".r val ...
gruenewa's user avatar
  • 1,686
3 votes
1 answer
3k views

Scala alternative cases match syntax with different type of extracted value

object NoSense { def main(args: Array[String]) { val value = "true" match { case value @ (IntValue(_) | BooleanValue(_)) => value } require(value == true) } } ...
jilen's user avatar
  • 5,703
3 votes
1 answer
144 views

Why and how to migrate case class to extractor in Scala

In the Programming in Scala discussion on when to use case classes and when extractors, they say Extractors break [the] link between data representations and patterns ... This property is called ...
joel's user avatar
  • 6,856
1 vote
1 answer
63 views

No pattern match for Seq[Seq[T]]

def example1(sss:Seq[Seq[String]]) = sss match { case Seq(a, b) :+ Seq(c, d) :+ tail => true } val res1 = example1(Seq(Seq("a", "b"), Seq("c","d"))) // scala.MatchError: List(List(a, b), List(c, ...
Luigi Plinge's user avatar
  • 50.9k
1 vote
1 answer
717 views

Fixing a match clause warning by getting rid of a type parameter

i want to implement the following exhaustive match, but i can't figure out to remove the type parameter and thus the warning that it is erased: sealed trait Q[+V] case object QEmpty extends Q[Nothing]...
0__'s user avatar
  • 67k
1 vote
2 answers
2k views

How to match multiple case classes and extract same (-named) arguments?

Consider the following contrived example of implementing a unary & binary operations on real number expressions. abstract class DoubleE case class Negate(x: DoubleE) extends DoubleE case class ...
eold's user avatar
  • 6,012
1 vote
1 answer
92 views

Problems with scala regex extractor

I have a problem with the regex extrator, this is my regex val regex = """(some\/params\/results\/\b[A-Z]{3}\/[A-Z]{3}\b\/)*""".r And when I try to do this: val regex(res) = "some/params/results/...
IgnacioPL's user avatar
1 vote
2 answers
346 views

How to use case class define a extractor?

I want to use case class match Seq[Byte] as List defined, but a compiler error occurred. use case class with compiler error case class :: (head: Byte, tail: Seq[Byte]) def ...
LoranceChen's user avatar
  • 2,473
1 vote
1 answer
154 views

Email Extractor Java Pattern excluding characters before a dot

I am currently using an email extractor which is working well, but I would like to change its pattern which is: [^a-zA-Z0-9-](?<num>[a-zA-Z0-9_-]{2,20}@[A-Za-z0-9_-]{3,20}\.[\.a-zA-Z0-9_-]+)[^a-...
Lola1234's user avatar
0 votes
3 answers
356 views

Scala: Workaround for unparameterizable extractor

Since extractors cannot take custom parameters (as answered in Stack Overflow: Can extractors be customized...), I try to find an alternative way of solving the following problem. I have a lot of ...
ideaboxer's user avatar
  • 3,973
0 votes
2 answers
231 views

How does the extractor work when the `unapply` function returns Boolean instead of Option?

I learned about extractors from the stairway book: object Twice { def apply(x: Int) = x * 2 def unapply(x: Int) = if(x % 2 == 0) Some(x / 2) else None } // outside pattern ...
qed's user avatar
  • 22.7k
0 votes
0 answers
250 views

Kotlin Array/List extractor in Pattern Matching

In Scala one can extract array elements in a pattern matching statement. For example, I want to take the first two elements of a string input: private def parseFieldSize(s: String): Option[(Int, Int)...
pumpump's user avatar
  • 381