pattern-matching

Remove Duplicates from the List recursively

[亡魂溺海] 提交于 2019-12-11 14:38:45
问题 I want to remove duplicates from the list recursively using pattern matching with Scala here is my input val xs = List(1,2,3,4,6,3,2,7,9,4) Tried code: def removeDuplicates(xs : List[Int]) : List[Int] = xs match { case Nil =>Nil case x::ys => { if(ys.contains(x)){ removeDuplicates(ys) } else { } /// ??? } } I was stuck at the question mark, how to appened my result to the mutable list and return it. Thank you. 回答1: You're close: def removeDuplicates(xs : List[Int]) : List[Int] = xs match {

Match string in XText regardless of upper/lower case

落花浮王杯 提交于 2019-12-11 13:33:31
问题 I want to create a rule in XText that matches to a string, but does not care in what case the string is. For example, I want it to match against both "DUCK", "DucK" and "duck". Is there a more simple way of doing it than covering all cases, like: Rule: 'Duck'|'DucK'|'DuCK'|... and so on ; 回答1: There is a flag that you can configure in the mwe2 workflow. Replace the XtextAntlrGeneratorFragment by the org.eclipse.xtext.generator.parser.antlr.ex.rt.AntlrGeneratorFragment and pass options = {

Grep to match all the patterns from a file

你。 提交于 2019-12-11 13:08:59
问题 I have a set of pattern strings in a file and I want to have grep find lines that match all of the patterns (logical AND). In my example I want to only find the last line containing both 1 AND 2 (that is, 12 ), but what actually happens in the match is a logical OR, so all lines are returned: test_file.txt: 1 2 12 pattern.f: 1 2 And I run this command: $ grep -f pattern.f test_file.txt 1 2 12 Can I make grep do what I want in one line? I don't really want to have to write a script to read the

Conflicting Definitions in Pattern Matching

本秂侑毒 提交于 2019-12-11 12:34:27
问题 I just started learning Haskell and I ran into a problem in 2-adic type classes. Here's the important code: data Rectangle = NoRect | Rect (Float,Float) (Float,Float) | Pane deriving (Show) class Collision s1 s2 where collides :: s1 -> s2 -> Bool instance (Collision Rectangle Rectangle) where collides (Rect (aOrX, aOrY) (aCorX, aCorY)) (Rect (bOrX,bOrY) (bCorX,bCorY)) = ... collides Pane _ = True ... The compiler (GHC 6.12.1) now complains about Conflicting definitions for 'collides' I don't

MYSQL: Using GROUP BY with string literals

空扰寡人 提交于 2019-12-11 12:13:59
问题 I have the following table with these columns: shortName, fullName, ChangelistCount Is there a way to group them by a string literal within their fullName? The fullname represents file directories, so I would like to display results for certain parent folders instead of the individual files. I tried something along the lines of: GROUP BY fullName like "%/testFolder/%" AND fullName like "%/testFolder2/%" However it only really groups by the first match.... Thanks! 回答1: Perhaps you want

Scala returning coordinate as tuple

℡╲_俬逩灬. 提交于 2019-12-11 12:09:17
问题 I have a 7 by 7 grid, and I have to parse a string that fills in the grid. So if I have a string with 49 elements, I need to convert the string into a usable grid. The grid is represented with a map that looks like this: Map[(Int, Int), List[Int])] Here's what I have so far: def parseHelper(str: String, x: Int, y: Int, lst: Map[(Int, Int), List[Int]]): Map[(Int,Int), List[Int]] = { str match{ case "" => lst case _ => if (x == 6){ if (str.charAt(0).equals('.')){ parseHelper(str.substring(1), 0

Python finding most common pattern in list of strings

 ̄綄美尐妖づ 提交于 2019-12-11 11:57:10
问题 I have a large list of API calls stored as strings, which have been stripped of all common syntax('htttp://', '.com', '.', etc..) I would like to return a dictionary of the most common patterns which have a length > 3, where the keys are the found patterns and values are the number of occurrences of each pattern. I've tried this: calls = ['admobapioauthcert', 'admobapinewsession', 'admobendusercampaign'] >>> from itertools import takewhile, izip >>> ''.join(c[0] for c in takewhile(lambda x:

Extracting columns from a file

隐身守侯 提交于 2019-12-11 11:21:21
问题 I have a file containing a big table, something like that : Header1 Header2 Header3 ... Header8031 Value1 Value2 Value3 .... Value8031 . . Value1 Value2 Value3 ... Value8031 In another file I have a list with some headers of the previous table. Header1 Header3000 Header5 Header200 Header10 I want to extract the information in the table only for the headers in the list. In other words, getting the columns that match with the headers on the list. [matching the list with the columns id on the

Non overlapping pattern matching with gap constraint in python

廉价感情. 提交于 2019-12-11 10:38:44
问题 I want to find total no. of non-overlapping matches of a pattern appearing in a sequence, with the gap constraint 2. Eg. 2982f 2982l 2981l is a pattern found using some algorithm. I have to find the total # of this pattern appearing in a sequence such as 2982f 2982f 2982l 2982l 2981l 3111m 3171f 2982f 2982l 2981l … , where the max gap constraint is 2. Gap constraint 2 means that between the pattern 2982f 2982l 2981l , maximum of 2 other words allowed. And, the main thing is all these matches

Haskell pattern matching conundrum

萝らか妹 提交于 2019-12-11 09:44:33
问题 I was trying to search through a list of pairs that could have the element ("$", Undefined) in it at some arbitrary location. I wanted to ONLY search the part of the list in front of that special element, so I tried something like this (alreadyThere is intended to take the element n and the list xs as arguments): checkNotSameScope :: Env -> VarName -> Expr -> Expr checkNotSameScope (xs:("$", Undefined):_) n e = if alreadyThere n xs then BoolLit False else BoolLit True But that does not work;