pattern-matching

Using incomplete pattern matching as filter?

邮差的信 提交于 2019-12-05 09:32:03
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 case not covered by the pattern(s). Unmatched elements will be ignored. As the ignoring of unmatched

complex transposing of columns with pure sed

纵饮孤独 提交于 2019-12-05 08:21:42
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,190,191,390 190104555 00C7,00D1,00E1,0454,0462 APM00065101435 391,392 What have I tried? Some of my non

Counting the number of substrings

◇◆丶佛笑我妖孽 提交于 2019-12-05 08:02:28
I would like to ask if there is an algorithm for counting the number of discrete occurencies of a substring in a string in O(n) time. [EDIT 17/11/2013: Count the leaf nodes. Thanks Vinicius Pinto!] You can build a suffix tree on the text in linear time. Then, search for your substring in the suffix tree; when you find it, count the number of leaf nodes beneath the matching node. This is O(m + k) for a substring of length m that appears k times (add an n term for building the suffix tree). Or, you can precalculate the number of descendants for each node in the tree using a depth-first traversal

Search pattern in string using wildcard in Delphi?

扶醉桌前 提交于 2019-12-05 07:27:18
I used to use HYPERSTR library for string processing routine. Now I use newer Delphi. I need to search a pattern in a string, for example the old function is function IsMatchEx(const Source, Search:AnsiString; var Start:integer) : Integer; . Actually I don't need the result value, I just wanna know if the pattern match with the string or not. My old code (returns TRUE): var StartPos: integer; FoundPos: integer; begin StartPos := 1; FoundPos := IsMatchEx('abcdef', 'abcd?f', StartPos); if FoundPos > 0 then showmessage('match'); end; I see that Delphi XE has TRegEx but I stil don't understand to

match tuple with null

和自甴很熟 提交于 2019-12-05 07:23:45
I don't understand why the following case doesn't match. Null should be an instance of Any, but it doesn't match. Can someone explain what is going on? val x = (2, null) x match { case (i:Int, v:Any) => println("got tuple %s: %s".format(i, v)) case _ => println("catch all") } prints catch all Thanks. This is exactly as specified. Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms: * A reference to a class C, p.C, or T#C. This type pattern matches any non-null instance of the given class. It's interesting that so much relevance has

patterns for “symmetric” functions

我是研究僧i 提交于 2019-12-05 07:12:57
Trying out this new stackoverflow thing, as suggested :) This is not really haskell specific, but it's clearest in haskell. Here's a pattern that comes up every once and a while: a function takes two arguments which it treats symmetrically. mappends frequently have this property. An example: -- | Merge sorted lists of ranges. merge :: (Ord n) => [(n, n)] -> [(n, n)] -> [(n, n)] merge [] r2 = r2 merge r1 [] = r1 merge r1@((s1, e1) : rest1) r2@((s2, e2) : rest2) | e1 < s2 = (s1, e1) : merge rest1 r2 | e2 < s1 = (s2, e2) : merge r1 rest2 | s1 >= s2 && e1 <= e2 = merge rest1 r2 -- 1 within 2 | s2

Pattern matching in a data frame context

梦想与她 提交于 2019-12-05 06:30:06
I have a data frame, the first 5 lines of which looks as follows: Sample CCT6 GAT1 IMD3 PDR3 RIM15 001 0000000000 111111111111111111111 010001000011 0N100111NNNN 01111111111NNNNNN 002 1111111111 111111111111111111000 000000000000 0N100111NNNN 00000000000000000 003 0NNNN00000 000000000000000000000 010001000011 000000000000 11111111111111111 004 000000NNN0 11100111111N111111111 010001000011 111111111111 01111111111000000 005 0111100000 111111111111111111111 111111111111 0N100111NNNN 00000000000000000 The full data set has 2000 samples. I am trying to write code that will allow me to tell if the

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

↘锁芯ラ 提交于 2019-12-05 06:25:40
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 JavaScript script does not work: var url = window.location; if ( url.match( /^(http:)\/\/(.+\.)?

Pattern matching on List[T] and Set[T] in Scala vs. Haskell: effects of type erasure

拥有回忆 提交于 2019-12-05 06:23:53
Would the Haskell equivalent of the code below produce correct answers? Can this Scala code be fixed to produce correct answers ? If yes, how ? object TypeErasurePatternMatchQuestion extends App { val li=List(1,2,3) val ls=List("1","2","3") val si=Set(1,2,3) val ss=Set("1","2","3") def whatIsIt(o:Any)=o match{ case o:List[Int] => "List[Int]" case o:List[String] => "List[String]" case o:Set[Int] => "Set[Int]" case o:Set[String] => "Set[String]" } println(whatIsIt(li)) println(whatIsIt(ls)) println(whatIsIt(si)) println(whatIsIt(ss)) } prints: List[Int] List[Int] Set[Int] Set[Int] but I would

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

一曲冷凌霜 提交于 2019-12-05 06:19:26
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 length(l)=5; by looking at table[l-1]=3; As per KMP algorithm we can skip length up to 2 chars and can