How to pattern match on generic type in Scala?

Maybe this will help def matchContainer[A: Manifest](c: Container[A]) = c match { case c: Container[String] if manifest <:< manifest[String] => println(c.value.toUpperCase) case c: Container[Double] if manifest <:< manifest[Double] => println(math.sqrt(c.value)) case c: Container[_] => println(“other”) } Edit: As Impredicative pointed out, Manifest is deprecated. Instead you could do the following: import reflect.runtime.universe._ def matchContainer[A: TypeTag](c: … Read more

In Scala, why is NaN not being picked up by pattern matching?

It is a general rule how 64-bit floating point numbers are compared according to IEEE 754 (not Scala or even Java related, see NaN): double n1 = Double.NaN; double n2 = Double.NaN; System.out.println(n1 == n2); //false The idea is that NaN is a marker value for unknown or indeterminate. Comparing two unknown values should always … Read more

Pattern matching string prefixes in Haskell

myfunc (‘t’:’o’:’a’:’s’:’t’:’e’:’r’ : restOfString) = … Using a normal pattern match works, but gets bothersome as the prefix string gets longer. {-# LANGUAGE PatternGuards #-} import Data.List myFunc string | Just restOfString <- stripPrefix “toaster” string = — do something special myFunc string = — do the default case here Using a library function instead … Read more

Python regex string matching?

re.match implicitly adds ^ to the start of your regex. In other words, it only matches at the start of the string. re.search will retry at all positions. Generally speaking, I recommend using re.search and adding ^ explicitly when you want it. http://docs.python.org/library/re.html

String pattern matching best practice

In terms of syntax, you can modify just a tiny bit you case statements: case url if url.startsWith(“jdbc:mysql:”) => “com.mysql.jdbc.Driver” This simply binds the value url to the pattern expression (which is also url) and adds a guard expression with a test. That should make the code compile. To make it a little bit more … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)