pattern-matching

Pattern matching for strings independent from symbols

那年仲夏 提交于 2019-12-04 13:14:18
I have need for an algorithm which can find pre-defined patterns in data (which is present in the form of strings) independent from the actual symbols/characters of the data and the pattern. I only care about the relations between the symbols, not the symbols themselves. It is also legal to have different pattern symbols for the same symbol in the data. The only thing the pattern matching algorithm has to enforce is that multiple occurences of the same symbol in the pattern are preserved. To give you an example: The pattern is abca , so the first and the last letter are the same. For my

Flattening nested lists of the same type

你离开我真会死。 提交于 2019-12-04 13:04:55
Let's say I want to flatten nested lists of the same type... For example ListA(Element(A), Element(B), ListA(Element(C), Element(D)), ListB(Element(E),Element(F))) ListA contains nested list of the same type ( ListA(Element(C), Element(D)) ) so I want to substitute it with the values it contains, so the result of the upper example should look like this: ListA(Element(A), Element(B), Element(C), Element(D), ListB(Element(E),Element(F))) Current class hierarchy: abstract class SpecialList() extends Exp { val elements: List[Exp] } case class Element(name: String) extends Exp case class ListA

F# How to tokenise user input: separating numbers, units, words?

。_饼干妹妹 提交于 2019-12-04 12:38:04
I am fairly new to F#, but have spent the last few weeks reading reference materials. I wish to process a user-supplied input string, identifying and separating the constituent elements. For example, for this input: XYZ Hotel: 6 nights at 220EUR / night plus 17.5% tax the output should resemble something like a list of tuples: [ ("XYZ", Word); ("Hotel:", Word); ("6", Number); ("nights", Word); ("at", Operator); ("220", Number); ("EUR", CurrencyCode); ("/", Operator); ("night", Word); ("plus", Operator); ("17.5", Number); ("%", PerCent); ("tax", Word) ] Since I'm dealing with user input, it

What is wrong with my regex Pattern to find recurring cycles in Python?

血红的双手。 提交于 2019-12-04 12:15:37
I want to match any string that has a recurring cycle. Like in this data: 3333333333333333333333333333333333333333 / 1 digit cycle(3) 1666666666666666666666666666666666666666 / 1 digit cycle(6) 1428571428571428571428571428571428571428 / 6 digit cycle(142857) 1111111111111111111111111111111111111111 / 1 digit cycle(1) 0909090909090909090909090909090909090909 / 2 digit cycle(09) 0834522467546323545411673445234655345222 / no cycle 0769230769230769230769230769230769230769 / 6 digit cycle(769230) 0714285714285714285714285714285714285714 / 6 digit cycle(714285)

extract a string after a pattern

你。 提交于 2019-12-04 11:52:49
问题 I want to extract the numbers following client_id and id and pair up client_id and id in each line. For example, for the following lines of log, User(client_id:03)) results:[RelatedUser(id:204, weight:10),_RelatedUser(id:491,_weight:10),_RelatedUser(id:29, weight: 20) User(client_id:04)) results:[RelatedUser(id:209, weight:10),_RelatedUser(id:301,_weight:10) User(client_id:05)) results:[RelatedUser(id:20, weight: 10) I want to output 03 204 03 491 03 29 04 209 04 301 05 20 I know I need to

Pattern Match on Case Objects with Type Members

五迷三道 提交于 2019-12-04 11:39:24
问题 Scala has a nice feature to infer type parameter inside the pattern match. It also checks pattern match exhaustiveness. For example: sealed trait PField[T] case object PField1 extends PField[String] case object PField2 extends PField[Int] def getValue[X](f: PField[X]): X = f match { case PField1 => "aaa" case PField2 => 123 } Is it possible to achieve the same but using type members instead of type parameters? sealed trait Field { type T } case object Field1 extends Field { type T = String }

Find possible duplicates in two columns ignoring case and special characters

元气小坏坏 提交于 2019-12-04 11:37:31
问题 Query SELECT COUNT(*), name, number FROM tbl GROUP BY name, number HAVING COUNT(*) > 1 It sometimes fails to find duplicates between lower case and upper case. E.g.: sunny and Sunny don't show up as a duplicates. So how to find all possible duplicates in PostgreSQL for two columns. 回答1: lower()/ upper() Use one of these to fold characters to either lower or upper case. Special characters are not affected: SELECT count(*), lower(name), number FROM tbl GROUP BY lower(name), number HAVING count(

scala pattern match a function - how to get around type erasure

我是研究僧i 提交于 2019-12-04 11:18:55
I would like to pattern match a function, the problem is type erasure. Notice how in the snippet below, despite the warning issued a match occurs and a "wrong" one at that. scala> def f1 = ()=>true f1: () => Boolean scala> val fl = f1 fl: () => Boolean = <function0> scala> scala> fl match { | case fp :Function0[Boolean] => 1 | case _ => 2 | } res8: Int = 1 scala> scala> fl match { | case fp :Function0[String] => 1 | case _ => 2 | } <console>:11: warning: fruitless type test: a value of type () => Boolean cannot also be a () => String (but still might match its erasure) case fp :Function0

Algorithm to detect redundant rules

我只是一个虾纸丫 提交于 2019-12-04 10:55:26
问题 I am looking for an algorithm to detect redundant rules. Rules have a fixed number of input parameters and each parameter has a distinct domain. Consider three rule parameters Color, Material and Size: Color : Red, Green, Blue Material : Wood, Glass, Aluminium Size : Small, Medium, Large Each rule can match on multiple values of a parameter or match on any value. The first rule that matches all parameters values is selected. There are no negation rules, but the domain is fixed, so a negation

Swift pattern matching with enum and Optional tuple associated values

萝らか妹 提交于 2019-12-04 10:05:46
I'm currently using Alamofire and I use an enum to describe the API I used as advised in the readme. The endpoints are represented as follows: public enum API { case GetStops(stopCode:String?) case GetPhysicalStops case GetLinesColors case GetNextDepartures(stopCode:String, departureCode:String?, linesCode:String?, destinationsCode:String?) } The optional parameters are mutually exclusive: public var URLRequest: NSMutableURLRequest { let result:(path:String, parameters:[String:AnyObject]?) = { switch self { case .GetStops(let stopCode) where stopCode != nil : return ("GetStops.json", [