pattern-matching

Pattern matching key in erlang maps

谁说胖子不能爱 提交于 2020-01-20 08:22:46
问题 I have a map of form shown below: Map = #{#{country=>"India"} => #{rank => 1}}. I am trying to match it as follows: 1. #{Key := V} = Map. OR 2. #{#{country := Country} := #{rank := Rank}} = Map. But its not working for me. Any help as to how it can be done? 回答1: When matching key-value associations from maps the key expression must be an expression with literals or bound variables, see the documentation of maps (section Maps in Patterns ). The problem with a match expression like: #{Key := V}

How to use cascade.xml for object recognition in openCV

孤街醉人 提交于 2020-01-17 03:54:27
问题 I've generated cascade xml in openCV using haar training module.. Now how can I use it for Object recognition.? Please help.!! _/_ 回答1: Following opencv doc on Object Detection, you have to create the cascade detector object and ::load the cascade you want to apply (the xml file you have generated). ::detectMultiScale is used to fill a std::vector<cv::Rect> of detected object from current frame by sliding windows of different scales and sizes and merging high confident close samples. Code

Why do I get “non-exhaustive patterns in function”?

回眸只為那壹抹淺笑 提交于 2020-01-17 02:21:09
问题 In the following code Haskell complains about Non-exhaustive patterns in function prime' prime :: Int -> [Int] prime x = prime' [2..x] where prime' (p:ps)= p : prime' [x | x <- ps, mod x p > 0 && prime'' x [2..div x 2]] prime'' _ [] = True prime'' n (x:xs) | mod n x == 0 = False | otherwise = prime'' n xs prime' []=[] I can't find my mistake. Could someone explain why this happens, and what it means? 回答1: Indentation. The last line defines another function called prime' . Therefore, prime

Regular expression not extracting the exact pattern

一个人想着一个人 提交于 2020-01-16 11:26:30
问题 I am working in Java to read a string of over 100000 characters. I have a list of keywords, that I search the string for, and if the string is present I call a function which does some internal processing. The kind of keyword I have is "face", for example - I wish to get all the patterns where I have matches for "faces" not "facebook". I can accept a space character behind the face in the string so if in a string I have a match like " face" or " faces" or "face " or " faces" i can accept that

How to store sets, to find similar patterns fast?

喜夏-厌秋 提交于 2020-01-16 10:09:34
问题 (This is no homework and no work issue. It's just my personal interest/occupation and completly fictional. But I am interested in a good algorithm or data structure.) Let's assume, that I would run a dating site. And my special feature would be that the singles were matched by movie taste . (Why not?) In that case I would need a way to store the movie ratings for each user. (So far no problem.) And I would need a data structure to find the best fitting user. The distance between two taste

How to perform multiple styles of pattern matching?

╄→гoц情女王★ 提交于 2020-01-16 01:09:07
问题 Just started to play with F#. As terrible as I'm with it now, I do not to know to search for a similar thread too. This is what I'm trying to do: let test animal = if animal :? Cat //testing for type then "cat" elif animal :? Dog //testing for type then "dog" elif animal = unicorn //testing value equality then "impossible" else "who cares" Basically it involves type test pattern matching along with other conditional checks. I can get the first part (type checking) done like this: let test

Find common patterns across strings and group them based on the pattern [duplicate]

天涯浪子 提交于 2020-01-15 07:09:07
问题 This question already has answers here : How can I detect common substrings in a list of strings (9 answers) Closed 4 years ago . How to automatically extract the common characters or common string from a set of input strings? is there an algorithm that does this? I am trying to figure out how to parse 1000 input strings and automatically create groups of string based on the largest matching patterns. Is there a library in ruby which does this? Sample Input What is your name? Who wrote this

Regex substring one mismatch in any location of string

孤人 提交于 2020-01-15 07:08:07
问题 Can someone explain why the code below returns an empty list: >>> import re >>> m = re.findall("(SS){e<=1}", "PSSZ") >>> m [] I am trying to find the total number of occurrences of SS (and incorporating the possibility of up to one mismatch ) within PSSZ . I saw a similar example of code here: Search for string allowing for one mismatch in any location of the string 回答1: You need to remove e<= chars present inside the range quantifier. Range quantifier must be of , {n} . Repeats the previous

`case` that refines arguments

孤街浪徒 提交于 2020-01-15 02:00:28
问题 In this answer on a question about the totality checker, a workaround involving using case instead of with was recommended. However, in situations where the result of the match refines the type of other arguments, this transformation is not straightforward to make. For example, given the following definitions: data IsEven : Nat -> Nat -> Type where Times2 : (n : Nat) -> IsEven (n + n) n data IsOdd : Nat -> Nat -> Type where Times2Plus1 : (n : Nat) -> IsOdd (S (n + n)) n total parity : (n :

Is it a rule that unapply will always return an Option?

爱⌒轻易说出口 提交于 2020-01-14 08:40:49
问题 I tried to create an unapply method to use in pattern matching, and I tried to make it return something different than Option , however, Eclipse shows that as an error. Is it a rule that unapply must return an Option[T] ? EDIT: here's the code I'm trying to use. I switched the code from the previous section so that unapply returns a Boolean import java.util.regex._ object NumberMatcher { def apply(x:String):Boolean = { val pat = Pattern.compile("\\d+") val matcher = pat.matcher(x) return