pattern-matching

Match the nth longest possible string in Perl

北城以北 提交于 2019-12-07 05:34:38
问题 The pattern matching quantifiers of a Perl regular expression are "greedy" (they match the longest possible string). To force the match to be "ungreedy", a ? can be appended to the pattern quantifier (*, +). Here is an example: #!/usr/bin/perl $string="111s11111s"; #-- greedy match $string =~ /^(.*)s/; print "$1\n"; # prints 111s11111 #-- ungreedy match $string =~ /^(.*?)s/; print "$1\n"; # prints 111 But how one can find the second, third and .. possible string match in Perl? Make a simple

Using incomplete pattern matching as filter?

余生颓废 提交于 2019-12-07 04:56:17
问题 Suppose I have the following code: type Vehicle = | Car of string * int | Bike of string let xs = [ Car("family", 8); Bike("racing"); Car("sports", 2); Bike("chopper") ] I can filter above list using incomplete pattern matching in an imperative for loop like: > for Car(kind, _) in xs do > printfn "found %s" kind;; found family found sports val it : unit = () but it will cause a: warning FS0025: Incomplete pattern matches on this expression. For example, the value 'Bike (_)' may indicate a

Haskell type and pattern matching question: extracting fields from a data type

馋奶兔 提交于 2019-12-07 04:49:54
问题 I'm new to Haskell and working my way through the Write Yourself a Scheme in 48 Hours project and I came upon an instance where I wanted to get the underlying type out of a data type and I'm not sure how to do it without writing conversions for each variant in the type. For example, in the data type data LispVal = Atom String | List [LispVal] | DottedList [LispVal] LispVal | Number Integer | String String | Bool Bool | Double Double I want to write something like: (I know this doesn't work)

Pattern Matching of Units of Measure in F#

余生长醉 提交于 2019-12-07 04:49:49
问题 This function: let convert (v: float<_>) = match v with | :? float<m> -> v / 0.1<m> | :? float<m/s> -> v / 0.2<m/s> | _ -> failwith "unknown" produces an error The type 'float<'u>' does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion. Is there any way how to pattern match units of measure? 回答1: As @kvb explains in detail, the problem is that units of measure are a part of the type. This means that float<m> is different type than float<m/s> (and

Scala Pattern Matching pretty printed

不羁岁月 提交于 2019-12-07 03:20:57
问题 Is that possible to somehow marshall PartialFunction (let's assume it will always contains only one case) into something human-readable ? Let's say we have collection of type Any (messages: List[Any]) and number of PartialFuntion[Any, T] defined using pattern matching block. case object R1 case object R2 case object R3 val pm1: PartialFunction[Any, Any] = { case "foo" => R1 } val pm2: PartialFunction[Any, Any] = { case x: Int if x > 10 => R2 } val pm3: PartialFunction[Any, Any] = { case x:

Reasoning behind shifting over the text whem mismatch occurs in KMP algorithm?

牧云@^-^@ 提交于 2019-12-07 03:05:32
问题 I have been trying to understand KMP algorithm. Still I didn't get the clear understanding of reasoning behind kmp algorithm. Suppose my text is bacbababaabcbab and pattern is abababca . By using the rule of length of longest proper prefix of sub(pattern) that matches the proper suffix of sub(pattern) , I filled my table[] . a b a b a b c a 0 0 1 2 3 4 0 1 Now I started applying KMP algorithm on the text with my pattern and table. After coming to index 4 of above text, we have a match of

complex transposing of columns with pure sed

我是研究僧i 提交于 2019-12-07 02:53:26
问题 I tried a couple of hours to find a pure sed solution for this question. Obviously, unfortunately I had not succeed. A really tricky question. Examples (from the awk question): input: aaa 111 aaa 222 aaa 333 bbb 444 bbb 555 ccc 666 output: aaa 111,222,333 bbb 444,555 ccc 666 input APM00065101435 189 APM00065101435 190 APM00065101435 191 APM00065101435 390 190104555 00C7 190104555 00D1 190104555 00E1 190104555 0454 190104555 0462 APM00065101435 391 APM00065101435 392 output APM00065101435 189

How can I use JavaScript to match a string inside the current URL of the window I am in?

时光总嘲笑我的痴心妄想 提交于 2019-12-07 02:35:27
问题 I have used the excellent gskinner.com/RegExr/ tool to test my string matching regex but I cannot figure out how to implement this into my JavaScript file to return true or false. The code I have is as follows: ^(http:)\/\/(.+\.)?(stackoverflow)\. on a url such as http://stackoverflow.com/questions/ask this would match (according to RegExr) http://stackoverflow. So this is great because I want to try matching the current window.location to that string, but the issue I am having is that this

Abusing pattern matching

六月ゝ 毕业季﹏ 提交于 2019-12-07 02:28:53
问题 I come from C# and find myself in love with the F# pattern matching syntax as it's simpler than C# switch and way more useful. I like to use it as much as possible, is there a performance or any other downside to using it in weird ways like in this example? match 0 with |_ when a<b -> a |_ -> b 回答1: In this particular example, there will be no performance penalty. It is very likely that performance penalty will also be absent in other cases, but to be absolutely sure you'll have to look at

Arithmetic Progression series in R

耗尽温柔 提交于 2019-12-07 02:03:27
I am new to this forum. I guess something like this has been asked before but, I am not really sure if that is what I want. I have a sequence like this, 1 2 3 4 5 8 9 10 12 14 15 17 18 19 So, what I wish to do is this, get all the numbers which form a series,i.e.the numbers that belonging to that set should all have a constant difference with the previous element, and also the minimum number of elements should be 3 in that set. i.e., I can see that (1,2,3,4,5) forms one such series in which numbers appear after an interval of 1 and the total size of this set is 5 which satisfies the minimum