Groovy/Java split string on parentheses “(“
println “Hello World(1)”.split(“\\(“);
println “Hello World(1)”.split(“\\(“);
The type pattern in its various forms: x is T y, case T y etc, always fails to match when x is null. This is because null doesn’t have a type, so asking “is this null of this type?” is a meaningless question. Therefore t is int? i or t is Nullable<int> i makes no … Read more
This is due to type-erasure. The JVM does not know of any type parameter, except on arrays. Because of that, Scala code can’t check whether an Option is an Option[Int] or an Option[String] — that information has been erased. You could fix your code this way, though: object Test { def test = { (Alice … Read more
| is not implemented in the library, it is interpreted by the Scala compiler. It builds a new pattern that is defined as the disjunction between two subpatterns that don’t bind any variable (although the newly formed pattern can itself be bound; i.e., you can write stuff like try { /*…*/ } catch { case … Read more
A very good description of compiling pattern matching is given in “The implementation of functional programming languages” by Simon Peyton Jones. It is a bit old but a very good book. It also contains, amongst other things, a description of compiling list comprehensions. The Erlang compiler uses both of these algorithms from the book.
Use regexes 😉 val Pattern = “(chat.*)”.r serv match { case Pattern(chat) => “It’s a chat” case _ => “Something else” } And with regexes you can even easily split parameter and base string: val Pattern = “(chat)(.*)”.r serv match { case Pattern(chat,param) => “It’s a %s with a %s”.format(chat,param) case _ => “Something else” … Read more
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.
You need slice patterns: fn vec_alt<T>(vals: Vec<T>) -> &’static str { match vals[..] { [a, b] => “two elements”, [a, b, c] => “three elements”, _ => “otherwise”, } }
Prolog pattern matching is based on unification, specifically the Martelli-Montanari Algorithm (minus the occurs check, by default). This algorithm matches values of the same position, binding variables on one side to a value at corresponding position on the other side. This kind of pattern matching could work both ways, therefore in Prolog you could use … Read more
Try using COLLATE Latin1_General_BIN rather than COLLATE Latin1_General_CS_AS