pattern-matching

Regular expression to match 12345

蹲街弑〆低调 提交于 2019-12-21 15:09:26
问题 Is there a regex to match a string of increasing contiguous numbers, e.g. 123, 56789, etc? I don't think it can be in regex but worth checking with folks here. 回答1: ^(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?(?:9$)?$ Result: http://rubular.com/r/JfJJ6ntEQG 回答2: ^(1|^)((2|^)((3|^)((4|^)((5|^)((6|^)((7|^)((8|^)((9|^))?)?)?)?)?)?)?)?$ Python demonstration: >>> import re >>> good = ['1', '12', '123', '23', '3', '4', '45',

Comparing F# discriminated union instances via pattern matching

痞子三分冷 提交于 2019-12-21 13:59:12
问题 Firstly, apologies for the poor title - I don't understand enough F# to describe the problem better. Consider this simple DU: type Money = | USD of decimal | GBP of decimal | EUR of decimal static member (+) (first: Money, second: Money) = match first, second with | USD(x), USD(y) -> USD(x + y) | GBP(x), GBP(y) -> GBP(x + y) | EUR(x), EUR(y) -> EUR(x + y) | _ -> failwith "Different currencies" I'm representing money in different currencies, and overloading the (+) operator so that I can

Scala pattern matching syntax

泄露秘密 提交于 2019-12-21 12:59:22
问题 I've been playing with scala pattern matching recently and was wondering whether there is a way to create an extractor inside of the case statement. The following code works, but you have to define the extractor first and assign it to a val: val Extr = "(.*)".r "test" match { case Extr(str) => println(str) } What I would like to do, or what I would like someone to confirm is impossible, is something like this: "test" match { case ("(.*)".r)(str) => println(str) } EDIT: In case anyone from the

Problem with Scala matching + scope

馋奶兔 提交于 2019-12-21 12:40:26
问题 Given the following code: case class ChangeSet(field:String, from:Object, to:Object) private var changed:List[ChangeSet] = Nil def change(field:String, from:Object, to:Object) { changed.find{ case ChangeSet(field,_,_) => true } match { case Some(ChangeSet(field,to,_)) => // do stuff case Some(_) => // do stuff case _ => // do stuff } } The line giving me trouble is Some(ChangeSet(field,to,_)) . It compiles but what seems to be happening is that Scala is filling it in as a placeholder for a

Problem with Scala matching + scope

爱⌒轻易说出口 提交于 2019-12-21 12:35:03
问题 Given the following code: case class ChangeSet(field:String, from:Object, to:Object) private var changed:List[ChangeSet] = Nil def change(field:String, from:Object, to:Object) { changed.find{ case ChangeSet(field,_,_) => true } match { case Some(ChangeSet(field,to,_)) => // do stuff case Some(_) => // do stuff case _ => // do stuff } } The line giving me trouble is Some(ChangeSet(field,to,_)) . It compiles but what seems to be happening is that Scala is filling it in as a placeholder for a

How to find pattern groups in boolean array?

与世无争的帅哥 提交于 2019-12-21 12:04:38
问题 Given a 2D array of Boolean values I want to find all patterns that consist of at least 2 columns and at least 2 rows. The problem is somewhat close to finding cliques in a graph. In the example below green cells represent "true" bits, greys are "false". Pattern 1 contains cols 1,3,4 and 5 and rows 1 and 2. Pattern 2 contains only columns 2 and 4, and rows 2,3,4. Business idea behind this is finding similarity patterns among various groups of social network users. In real world number of rows

SQL query to match one of multiple strings

独自空忆成欢 提交于 2019-12-21 09:19:13
问题 I have following data in table: +----------------------+----------------------------------------------------------+--------------+ | subscriber_fields_id | name | field_type | +----------------------+----------------------------------------------------------+--------------+ | 143 | Peshawar/Islamabad/Lahore/Swat/Mardan/Karachi | Job Location | | 146 | Karachi | Job Location | | 147 | Lahore and Karachi | Job Location | | 149 | Karachi, Mirpur Khas, Sukkur, Layyah, Gilgit, Charsaddah | Job

SQL query to match one of multiple strings

懵懂的女人 提交于 2019-12-21 09:18:50
问题 I have following data in table: +----------------------+----------------------------------------------------------+--------------+ | subscriber_fields_id | name | field_type | +----------------------+----------------------------------------------------------+--------------+ | 143 | Peshawar/Islamabad/Lahore/Swat/Mardan/Karachi | Job Location | | 146 | Karachi | Job Location | | 147 | Lahore and Karachi | Job Location | | 149 | Karachi, Mirpur Khas, Sukkur, Layyah, Gilgit, Charsaddah | Job

Switching on UIButton title: Expression pattern of type 'String' cannot match values of type 'String?!'

狂风中的少年 提交于 2019-12-21 09:07:44
问题 I'm trying to use a switch in an @IBAction method, which is hooked to multiple buttons @IBAction func buttonClick(sender: AnyObject) { switch sender.currentTitle { case "Button1": print("Clicked Button1") case "Button2": print("Clicked Button2") default: break } When I try the above, I get the following error: Expression pattern of type 'String' cannot match values of type 'String?!' 回答1: currentTitle is an optional so you need to unwrap it. Also, the type of sender should be UIButton since

C# 7 Pattern Match with a tuple

安稳与你 提交于 2019-12-21 07:41:41
问题 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