Scala: short form of pattern matching that returns Boolean

This is exactly why I wrote these functions, which are apparently impressively obscure since nobody has mentioned them. scala> import PartialFunction._ import PartialFunction._ scala> cond(“abc”) { case “def” => true } res0: Boolean = false scala> condOpt(“abc”) { case x if x.length == 3 => x + x } res1: Option[java.lang.String] = Some(abcabc) scala> condOpt(“abc”) … Read more

How to use SIFT algorithm to compute how similar two images are?

First, aren’t you supposed to be using vl_sift instead of sift? Second, you can use SIFT feature matching to find correspondences in the two images. Here’s some sample code: I = imread(‘p1.jpg’); J = imread(‘p2.jpg’); I = single(rgb2gray(I)); % Conversion to single is recommended J = single(rgb2gray(J)); % in the documentation [F1 D1] = vl_sift(I); … Read more

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

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

tech