pattern-matching

Pattern match a Seq with Range

梦想的初衷 提交于 2019-12-20 05:54:15
问题 Consider a piece of code: def foo(xs: Seq[Int]) = xs match { case Nil => "empty list" case head :: Nil => "one element list" case head :: tail => s"head is $head and tail is $tail" } val x1 = Seq(1,2,3) println(foo(x1)) val x2 = Seq() println(foo(x2)) val x3 = Seq(1) println(foo(x3)) val problem = 1 to 10 println(foo(problem)) A problem occurs, when we try to match a Range in foo(problem) . scala.MatchError: Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) (of class scala.collection.immutable.Range

Pattern match a Seq with Range

戏子无情 提交于 2019-12-20 05:54:04
问题 Consider a piece of code: def foo(xs: Seq[Int]) = xs match { case Nil => "empty list" case head :: Nil => "one element list" case head :: tail => s"head is $head and tail is $tail" } val x1 = Seq(1,2,3) println(foo(x1)) val x2 = Seq() println(foo(x2)) val x3 = Seq(1) println(foo(x3)) val problem = 1 to 10 println(foo(problem)) A problem occurs, when we try to match a Range in foo(problem) . scala.MatchError: Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) (of class scala.collection.immutable.Range

Find patterns of a file in another file and print out a corresponding field of the latter maintaining the order

自作多情 提交于 2019-12-20 04:16:34
问题 I've been trying for a while to solve this problem and I checked many posts (for example here Print lines in one file matching patterns in another file or here awk search for a field in another file) without really finding what I am looking for. I need the solution with bash tools like sed, grep, awk (no python, R,...) I have two files (much bigger than those): file1: 2 891299 0.50923964E-02 1248 4.713 1349.08 3 245857 0.57915542E-02 1335 4.671 1369.65 file2: 278 2645 2334659 0.75142 0.53123

Understanding the algorithm for pattern matching using an LCP array

非 Y 不嫁゛ 提交于 2019-12-20 03:52:07
问题 Foreword: My question is mainly an algorithmic question, so even if you are not familiar with suffix and LCP arrays you can probably help me. In this paper it is described how to efficiently use suffix and LCP arrays for string pattern matching. I understood SA and LCP work and how the algorithm's runtime can be improved from O(P*log(N)) (where P is the length of the pattern and N is length of the string) to O(P+log(N)) (Thanks to Chris Eelmaa's answer here and jogojapans answer here). I was

Understanding the algorithm for pattern matching using an LCP array

£可爱£侵袭症+ 提交于 2019-12-20 03:52:02
问题 Foreword: My question is mainly an algorithmic question, so even if you are not familiar with suffix and LCP arrays you can probably help me. In this paper it is described how to efficiently use suffix and LCP arrays for string pattern matching. I understood SA and LCP work and how the algorithm's runtime can be improved from O(P*log(N)) (where P is the length of the pattern and N is length of the string) to O(P+log(N)) (Thanks to Chris Eelmaa's answer here and jogojapans answer here). I was

Print line containing “word” python

房东的猫 提交于 2019-12-20 03:23:24
问题 I would like to print ONLY the line which contains "Server" in the below piece of output: Date: Sun, 16 Dec 2012 20:07:44 GMT Expires: -1 Cache-Control: private, max-age=0 Content-Type: text/html; charset=ISO-8859-1 Set-Cookie: PREF=ID=da8d52b67e5c7522:FF=0:TM=1355688464:LM=1355688464:S=CrK5vV-qb3UgWUM1; expires=Tue, 16-Dec-2014 20:07:44 GMT; path=/; domain=.google.com Set-Cookie: NID=67=nICkwXDM6H7TNQfHbo06FbvZhO61bzNmtOn4HA71ukaVDSgywlBjBkAR-gXCpMNo1TlYym

Getting multiple matches via regex

倖福魔咒の 提交于 2019-12-20 03:21:44
问题 I want to retrieve a strings from a global string via Matcher & Pattern using REGEX. String str = "<strong>ABC</strong>123<strong>DEF</strong>" Pattern pattern = Pattern.compile("<strong>(.*)</strong>"); Matcher matcher = pattern.matcher(str); My problem is that the matcher gives me just one match that is inside the global tag strong: ABC</strong>123<strong>DEF My objective is to get 2 matches: ABC DEF Thank you very match for you help. 回答1: You need a non greedy regex: Pattern pattern =

Scala: pattern matching over a reflect.runtime.universe.Type?

六眼飞鱼酱① 提交于 2019-12-20 02:59:04
问题 how can I do a pattern match over a reflect.runtime.universe.Type? def test(t: reflect.runtime.universe.Type) { t match { case Int => \\ ... case Double => \\ ... case String => \\ ... case _ => \\ ... } } This dosn't work, as the interpreter complains: error: pattern type is incompatible with expected type; found : Int.type required: reflect.runtime.universe.Type Note: if you intended to match against the class, try `case _: Int` case Int => // ... ^ Trying the suggestion does not work

Why is generic type in some expressions is matched as obj in F# pattern matching?

与世无争的帅哥 提交于 2019-12-20 01:38:51
问题 I have the following code: type Message<'a> = | Message of 'a let handleMessage message = match box message with | :? Message<_> -> printfn "Message" | _ -> printfn "Not message" let handleMessageEx message = match box message with | :? Message<int> -> printfn "Message" | _ -> printfn "Not message" handleMessage <| Message 1 handleMessage <| Message (1 :> obj) handleMessageEx <| Message 1 The output in F# Interactive is the following: Not message Message Message Why does the first statement

Replace square bracket using Powershell

橙三吉。 提交于 2019-12-19 21:22:30
问题 If you have a filename such as "Committee minutes [October 2010] - hq.doc", how do you get Powershell to replace the square brackets? The following doesn't work: ls -filter *`[*`]* | foreach -Process { Rename-Item $_ -NewName ($_.Name -replace '\[', '\(') | Rename-Item $_ -NewName ($_.Name -replace '\]', '\)')} I get the error: Rename-Item : Cannot rename because item at 'Committee minutes [October 2010] - hq.doc' does not exist. At line:1 char:53 + ls -filter *`[*`]* | foreach -Process {