Pattern matching identical values

This is called a nonlinear pattern. There have been several threads on the haskell-cafe mailing list about this, not long ago. Here are two: http://www.mail-archive.com/haskell-cafe@haskell.org/msg59617.html http://www.mail-archive.com/haskell-cafe@haskell.org/msg62491.html Bottom line: it’s not impossible to implement, but was decided against for sake of simplicity. By the way, you do not need if or case to work around this; … 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

How to match accented characters with a regex?

Instead of \w, use the POSIX bracket expression [:alpha:]: “blåbær dèjá vu”.scan /[[:alpha:]]+/ # => [“blåbær”, “dèjá”, “vu”] “blåbær dèjá vu”.scan /\w+/ # => [“bl”, “b”, “r”, “d”, “j”, “vu”] In your particular case, change the regex to this: NAME_REGEX = /^[[:alpha:]\s'”\-_&@!?()\[\]-]*$/u This does match much more than just accented characters, though. Which is a … Read more

Haskell GHC: what is the time complexity of a pattern match with N constructors?

A jump table is used, making the pattern-match a constant time operation. Unfortunately I’m unable to find an up-to-date citation for this, although this page mentions the implementation of Cmm-level switch statements as jump tables, and this old tagging design document uses a case on a Bool as an example, producing a jump table.

Check if string ends with certain pattern

This is really simple, the String object has an endsWith method. From your question it seems like you want either /, , or . as the delimiter set. So: String str = “This.is.a.great.place.to.work.”; if (str.endsWith(“.work.”) || str.endsWith(“/work/”) || str.endsWith(“,work,”)) // … You can also do this with the matches method and a fairly simple regex: … Read more

Why can’t I return a concrete subtype of A if a generic subtype of A is declared as return parameter?

Method’s right hand side (pattern matching) t match { case Empty => Empty case NonEmpty(elem, left, right) => if (elem < 0) throw new Exception else NonEmpty(elem, assertNonNegatve(left), assertNonNegative(right)) } means to check at runtime whether t is an instance of class Empty$ (object Empty) and then choose the first branch or otherwise whether t … Read more

How can I pull data out of an Option for independent use?

if let Some(origin) = resp.get(“origin”) { // use origin } If you can guarantee that it’s impossible for the value to be None, then you can use: let origin = resp.get(“origin”).unwrap(); Or: let origin = resp.get(“origin”).expect(“This shouldn’t be possible!”); And, since your function returns a Result: let origin = resp.get(“origin”).ok_or(“This shouldn’t be possible!”)?; Or with … Read more

Pattern matching on generic type in Scala

I would go with TypeTag if you’re on 2.10+ import reflect.runtime.universe._ class Observable[Foo] def X[T: TypeTag](ob: Observable[T]) = ob match { case x if typeOf[T] <:< typeOf[Double] => println(“Double obs”) case x if typeOf[T] <:< typeOf[Boolean] => println(“Boolean obs”) case x if typeOf[T] <:< typeOf[Int] => println(“Int obs”) } X(new Observable[Int]) // Int obs See … Read more