pattern-matching

Scala: pattern matching with reusable condition

早过忘川 提交于 2019-12-10 17:24:00
问题 Considering this case of pattern matching: foo match { case x if expensiveCalculation(x).nonEmpty => // do something with expensiveCalculation(x) ... } is it possible to "label" or reuse the expensiveCalculation(x) after the => ? Ideally I was expecting something like: foo match { case x val ec = expensiveCalculation(x); if ec.nonEmpty => // do something with ec ... } 回答1: You can write an extractor for the type of x (here assuming InputType ): object Expensive { def unapply(x: InputType):

How can I pattern-match a Vec<T> inside an enum field without nesting matches?

坚强是说给别人听的谎言 提交于 2019-12-10 17:17:17
问题 Pattern-matching a Vec<T> can be done by using either &v[..] or v.as_slice() . let x = vec![1, 2, 3]; match &x[..] { [] => println!("empty"), [_] => println!("one"), [..] => println!("many"), } If I have an enum with a field that contains the Vec I want to match on, I need to create a nested match inside the outer match arm: enum Test { Many(Vec<u8>), Text(String), } fn main() { let x = Test::Many(vec![1, 2, 3]); match x { Test::Text(s) => println!("{}", s), Test::Many(v) => match &v[..] { []

Numerical Pattern Matching

旧城冷巷雨未停 提交于 2019-12-10 17:05:18
问题 A project I'm researching requires some numerical pattern matching. My searches haven't turned up many relevant hits since most results tend to be around text pattern matching. The idea is we'll have certain wave patterns we'll need to be watching for and trying to match incoming data vs the wave database we will be building. Here is and example of one of the wave patterns we'll need to be matching against. alt text http://tmp.stayhealthy.com/wave.png There is clearly a pattern there, but the

Rust: “cannot move out of `self` because it is borrowed” error

那年仲夏 提交于 2019-12-10 16:59:31
问题 I'm trying to write a recursive method that adds an item to a tree and returns the tree node corresponding to that item. enum BstNode { Node(int, ~BstNode, ~BstNode), Leaf } impl BstNode { fn insert<'a>(&'a mut self, item: int) -> &'a mut BstNode { match *self { Leaf => { *self = Node(item, ~Leaf, ~Leaf); self }, Node(ref node_item, ref mut left, ref mut right) => match item.cmp(node_item) { Less => left.insert(item), Equal => self, Greater => right.insert(item) } } } } I'm bitten by the

Matching a hash character (#) with a regex

风格不统一 提交于 2019-12-10 16:39:51
问题 I have an XML document that contains a regular expression (so you don't need to escape with \). Basically I'm trying to match musical chord symbols, and this regex works fine, but refuses to match a hash: \b[A-G](m|b|\#|sus|\d)*?\b 回答1: The problem is that \b , the word boundary anchor, only matches between alphanumeric and non-alphanumeric characters, so it won't match after a # (unless that is itself followed by an alphanumeric). Use \b[A-G](?:m|b|#|sus|\d)*(?:\b|(?<=#)) No need to escape

PostgreSQL various clean up of string \ varchar

牧云@^-^@ 提交于 2019-12-10 15:43:26
问题 I have to clean up some varchar in the following manner: Remove special characters such as: !, @, #, $, %, ^, &, *, (, ), }, {, [, ], ",", ., ?, /, ', from a closed list. I've managed to do so with a mass use of replace\regexp_replace but I'm looking for something similar to the one in SQL server. Remove following numbers but not adjacent ones meaning: round 1 --> round round1 --> round1 round 12345 --> round round12345 --> round12345 Remove words out of a closed list of words such as: "and",

Pattern match is redundant

痞子三分冷 提交于 2019-12-10 15:37:08
问题 Haskell, Stack build tool. I have code: quote :: Char quote = '\'' doubleQuote :: Char doubleQuote = '\"' isBorder :: Char -> Bool isBorder quote = True isBorder doubleQuote = True isBorder _ = False It will be compiled without erors, but I see the messages during the compilation: D:\haskell\real\app\Main.hs:34:1: warning: [-Woverlapping-patterns] Pattern match is redundant In an equation for `isBorder': isBorder doubleQuote = ... D:\haskell\real\app\Main.hs:35:1: warning: [-Woverlapping

How do I use the box keyword in pattern matching?

若如初见. 提交于 2019-12-10 15:30:16
问题 This code is shown in The Rust Programming Language: #![feature(box_syntax, box_patterns)] fn main() { let b = Some(box 5); match b { Some(box n) if n < 0 => { println!("Box contains negative number {}", n); } Some(box n) if n >= 0 => { println!("Box contains non-negative number {}", n); } None => { println!("No box"); } _ => unreachable!(), } } But when I run it, the following error occurs: error[E0554]: #[feature] may not be used on the stable release channel I also tried fn main() { let b

Pattern Matching on a string

走远了吗. 提交于 2019-12-10 14:36:44
问题 I was wondering if there is a way to do something like this in c# 7 var test = "aaeag"; switch (test) { case test.StartsWith("a"): break; default: break; } Sadly it does not look like it possible. Is this correct or am I doing something wrong? 回答1: This is possible with C# 7, using a when guard: var test = "aaeag"; switch (test) { case var s when s.StartsWith("a"): break; default: break; } What your version of the code is doing is often referred to as active patterns . By eg defining the the

How to detect if a repeating pattern exists

↘锁芯ラ 提交于 2019-12-10 14:34:53
问题 My question isn't language specific... I would probably implement this in C# or Python unless there is a specific feature of a language that helps me get what I am looking for. Is there some sort of algorithm that anyone knows of that can help me determine if a list of numbers contains a repeating pattern? Let's say I have a several lists of numbers... [12, 4, 5, 7, 1, 2] [1, 2, 3, 1, 2, 3, 1, 2, 3] [1, 1, 1, 1, 1, 1] [ 1, 2, 4, 12, 13, 1, 2, 4, 12, 13] I need to detect if there is a