pattern-matching

C# 7 Pattern Match with a tuple

痴心易碎 提交于 2019-12-21 07:41:15
问题 Is it possible to use tuples with pattern matching in switch statements using c# 7 like so: switch (parameter) { case ((object, object)) tObj when tObj.Item1 == "ABC": break; } I get an error that says tObj does not exist in the current context . I have tried this as well: switch (parameter) { case (object, object) tObj when tObj.Item1 == "ABC": break; } This works fine: switch (parameter) { case MachineModel model when model.Id == "123": break; } 回答1: Remember that C#7 tuples are just

Can `match` in Racket have patterns with variables from an outer scope?

瘦欲@ 提交于 2019-12-21 07:14:04
问题 Consider the following example: #lang racket (match '(cat . doge) [`(,a . ,b) (match b [a #t] [_ #f])] [_ "Not a pair"]) This is what I might write if I wanted to match pairs where the head and tail are the same. This doesn't work though because the second a is bound as a new variable (and matches anything). Are there any pattern forms which allow me to use the previously bound a from the outer scope? I know this can be achieved in the following way (match* ('cat 'doge) [(a a) #t] [(_ _) #f])

how to match whitespace and alphanumeric characters in python

做~自己de王妃 提交于 2019-12-21 07:03:14
问题 I'm trying to match a string that has a space in the middle and alphanumeric characters like so: test = django cms I have tried matching using the following pattern: patter = '\s' unfortunately that only matches whitespace, so when a match is found using the search method in the re object, it only returns the whitespace, but not the entire string, how can I change the pattern so that it returns the entire string when finding a match? 回答1: import re test = "this matches" match = re.match('(\w+

PostgreSQL GIN index slower than GIST for pg_trgm?

。_饼干妹妹 提交于 2019-12-21 05:42:48
问题 Despite what all the documentation says, I'm finding GIN indexes to be significantly slower than GIST indexes for pg_trgm related searches. This is on a table of 25 million rows with a relatively short text field (average length of 21 characters). Most of the rows of text are addresses of the form "123 Main st, City". GIST index takes about 4 seconds with a search like select suggestion from search_suggestions where suggestion % 'seattle'; But GIN takes 90 seconds and the following result

How do I used matched arguments in elixir functions

扶醉桌前 提交于 2019-12-21 05:07:29
问题 Say I have two use cases for a function. Do something with users that have a subscription and something with users that don't have a subscriptions. def add_subscription(subscription_id, %User{subscription: nil}) # Do something with user here. And def add_subscription(subscription_id, user) How do I do pattern matching like this and also still use the user in the first function? Thanks! 回答1: You can bind the function argument to a variable: def add_subscription(subscription_id, %User

Search in 300 million addresses with pg_trgm

家住魔仙堡 提交于 2019-12-21 04:59:26
问题 I have 300 million addresses in my PostgreSQL 9.3 DB and I want to use pg_trgm to fuzzy search the rows. The final purpose is to implement a search function just like Google Map search. When I used pg_trgm to search these addresses, it cost about 30s to get the results. There are many rows matching the default similarity threshold condition of 0.3 but I just need about 5 or 10 results. I created a trigram GiST index: CREATE INDEX addresses_trgm_index ON addresses USING gist (address gist_trgm

How to check for multiple pattern matching in Perl

萝らか妹 提交于 2019-12-21 04:57:34
问题 Is there a way I can avoid using this for multiple pattern checks? Can I tore all the patterns in an array and check if it matches any pattern in the pattern array? Please consider the case when I have more than 20 pattern strings. if( ($_=~ /.*\.so$/) || ($_=~ /.*_mdb\.v$/) || ($_=~ /.*daidir/) || ($_=~ /\.__solver_cache__/) || ($_=~ /csrc/) || ($_=~ /csrc\.vmc/) || ($_=~ /gensimv/) ){ ... } 回答1: If you can use Perl version 5.10, then there is a really easy way to do that. Just use the new

Unexpected Scala pattern matching syntax

邮差的信 提交于 2019-12-21 04:22:32
问题 I had a List of Scala tuples like the following: val l = List((1,2),(2,3),(3,4)) and I wanted to map it in a list of Int where each item is the sum of the Ints in a the corresponding tuple. I also didn't want to use to use the x._1 notation so I solved the problem with a pattern matching like this def addTuple(t: (Int, Int)) : Int = t match { case (first, second) => first + second } var r = l map addTuple Doing that I obtained the list r: List[Int] = List(3, 5, 7) as expected. At this point,

Pattern matching a regex pattern in Haskell

我的梦境 提交于 2019-12-21 04:20:12
问题 In Scala, I have a regular expression pattern match like this: val Regex = """(\d{4})/(\d{2})/(\d{2})""".r val Regex(year, month, day) = "2013/01/06" The result is: year: String = 2013 month: String = 01 day: String = 06 How can I accomplish a similar result in Haskell? In other words, can I match a regular expression containing groups and assign the groups to identifiers? 回答1: This works for me: Prelude Text.Regex.Posix> "2013/01/06" =~ "([0-9]+)/([0-9]*)/([0-9]*)" :: (String,String,String,

“as” keyword in OCaml

六月ゝ 毕业季﹏ 提交于 2019-12-21 03:59:08
问题 In the answers for the tutorials for OCaml available at this site, some of the solutions, including the one for eliminating consecutive duplicates of list elements, is written as such: let rec compress = function | a :: (b :: _ as t) -> if a = b then compress t else a :: compress t | smaller -> smaller;; What is the relevance of the line a :: (b:: _ as t) ? Why can't I write it as a :: b :: t instead? 回答1: The t in b :: _ as t is bound to b :: _ . So the meaning is different. If you use the