pattern-matching

delete everything before pattern including pattern using awk or sed

你说的曾经没有我的故事 提交于 2019-12-10 14:20:39
问题 aaa aaaa aaaa aaaa sss ssss ssss ssss ddd dddd dddd dddd fff ffff ffff ffff abc pattern asd fde 111 222 333 444 555 666 777 888 999 000 Desired output : If the 111 222 333 444 555 666 777 888 999 000 回答1: Just set a flag whenever the pattern is found. From that moment on, print the lines: $ awk 'p; /pattern/ {p=1}' file 111 222 333 444 555 666 777 888 999 000 Or also awk '/pattern/ {p=1;next}p' file It looks for pattern in each line. When it is found, the variable p is set to 1. The tricky

Kotlin when() local variable introduction

孤人 提交于 2019-12-10 13:44:53
问题 Let's say I have an expensive function called doHardThings() which may return various different types, and I would like to take action based on the returned type. In Scala, this is a common use of the match construct: def hardThings() = doHardThings() match { case a: OneResult => // Do stuff with a case b: OtherResult => // Do stuff with b } I'm struggling to figure out how to do this cleanly in Kotlin without introducing a temporary variable for doHardThings() : fun hardThings() = when

Pattern matching on Parsers Success In Scala

拈花ヽ惹草 提交于 2019-12-10 13:43:01
问题 I'm new to Scala, and have been trying to use its excellent combinator parser library. I've been trying to get this code to compile: import scala.util.parsing.combinator._ ... val r:Parsers#ParseResult[Node] = parser.parseAll(parser.suite,reader) r match { case Success(r, n) => println(r) case Failure(msg, n) => println(msg) case Error(msg, n) => println(msg) } ... But I keep getting these errors: TowelParser.scala:97: error: not found: value Success case Success(r, n) => println(r) ^

Operator ~<~ in Postgres

◇◆丶佛笑我妖孽 提交于 2019-12-10 13:38:31
问题 (Originally part of this question, but it was bit irrelevant, so I decided to make it a question of its own.) I cannot find what the operator ~<~ is. The Postgres manual only mentions ~ and similar operators here, but no sign of ~<~ . When fiddling in the psql console, I found out that these commands give the same results: SELECT * FROM test ORDER BY name USING ~<~; SELECT * FROM test ORDER BY name COLLATE "C"; And these gives the reverse ordering: SELECT * FROM test ORDER BY name USING ~>~;

Pattern matching against generic type using 'flexible' type parameter

僤鯓⒐⒋嵵緔 提交于 2019-12-10 13:26:12
问题 match value with | :? list<#SomeType> as l -> l //Is it possible to match any list of a type derived from SomeType? | _ -> failwith "doesn't match" 回答1: No, it's unfortunately not possible to do something like this - the CLR doesn't provide any efficient way of doing that kind of type test. See How to cast an object to a list of generic type in F# and F# and pattern matching on generics in a non-generic method implementing an interface for a few (rather ugly) solutions. 回答2: As already

Fast r-contiguous matching (based on location similarities)

我的梦境 提交于 2019-12-10 13:19:55
问题 Assume that we have 2 equal size binary. A=101011110000 B=000010101111 How can we check their "R" contiguous matching based on similar location? For example if we set r=4 then the result will be false since there is no 4 contiguous similarities of locations. Both strings have 0000 or 1111 or 1010 but they are not in similar location . However if we set : A=1010111101111 B=1100101011111 The result will be true since the last 4 char (R) in both strings are equal to "1111". What is the fastest

php vertical regular expression search

爷,独闯天下 提交于 2019-12-10 13:17:12
问题 I've a string describing a matrix of n x m elements like this one: §inputmap = " ~~~~~~~~~~~~~~~~~~~~B~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~BBB........BBB~~~~~~~~~~~~~ ~~~~~~~~~~BB...............FBB~~~~~~~~~~ ~~~~~~~~BB....................BB~~~~~~~~ ~~~~~~BB.....F..................BB~~~~~~ ~~~~~BB.....................F.....B~~~~~ ~~~~B..............................B~~~~ ~~~B........F.......................B~~~ ~~BB.........F......................BB~~ ~~B................F.................BB~ ~BF...

Why typeclasses instead of just pattern matching?

﹥>﹥吖頭↗ 提交于 2019-12-10 13:08:36
问题 This is something of a philosophical question, but one I hope to have answered by official documentation or “word of god” (read: SPJ). Is there a specific reason that the Haskell committee chose to require explicit interfaces in the form of typeclasses rather than a more uniform solution based on pattern-matching? As an example, take Eq : class Eq a where (==), (/=) :: a -> a -> Bool x == y = not $ x /= y x /= y = not $ x == y instance Eq Int where (==) = internalIntEq Why could we not do

Better type checking on match in Scala

随声附和 提交于 2019-12-10 12:48:11
问题 scala> class A defined class A scala> class B defined class B scala> val a: A = new A a: A = A@551510e8 scala> a match { | case _: B => println("unlikely") | case _ => println("no match") | } no match In the example above shouldn't the compiler tell me that one of the cases can never match? A slightly more complicated example recently caught me out, leading to what felt like an unnecessary bug that should have been caught by the compiler. Edit: Just to be clearer about the question. Is this

Suppress exhaustive matching warning in OCaml

廉价感情. 提交于 2019-12-10 12:39:53
问题 I'm having a problem in fixing a warning that OCaml compiler gives to me. Basically I'm parsing an expression that can be composed by Bool , Int and Float . I have a symbol table that tracks all the symbols declared with their type: type ast_type = Bool | Int | Float and variables = (string, int*ast_type) Hashtbl.t; where int is the index used later in the array of all variables. I have then a concrete type representing the value in a variable: type value = | BOOL of bool | INT of int | FLOAT