pattern-matching

Separating an Output with a Tab / Space : Perl

陌路散爱 提交于 2019-12-22 09:49:58
问题 I am working with three text documents . The first one is the main input (Input 1) with words and the word type (Noun , Verb etc.) separated by a tab. Input 1 John N goes V to P school N . S Mary N comes V from P home N . S The second and third input text files look like this : Input 2 John Mary Input 3 to from My objective is to compare and match the second and third text files with the main input and get a output like this : Expected output: John N N goes V to P P school N . S Mary N N

Suggestion for solving fragile pattern matching

左心房为你撑大大i 提交于 2019-12-22 08:52:12
问题 I often need to match a tuple of values that should have the same constructor. The catchall _,_ always winds-up at the end. This of course is fragile, any additional constructor added to the type will compile perfectly fine. My current thoughts are to have matches that connect the first but not second argument. But, is there any other options? For example, type data = | States of int array | Chars of (char list) array let median a b = match a,b with | States xs, States ys -> assert( (Array

Implicit parameters won't work on unapply. How to hide ubiquitous parameters from extractors?

橙三吉。 提交于 2019-12-22 08:50:29
问题 Apparently unapply/unapplySeq in extractor objects do not support implicit parameters. Assuming here an interesting parameter a, and a disturbingly ubiquitous parameter b that would be nice to hide away, when extracting c. [ EDIT ]: It appears something was broken in my intellij/scala-plugin installation that caused this. I cannot explain. I was having numerous strange problems with my intellij lately. After reinstalling, I can no longer reprodce my problem. Confirmed that unapply/unapplySeq

unresolved flex record (need to know the names of ALL the fields in this context)

守給你的承諾、 提交于 2019-12-22 06:38:13
问题 I've been trying to create a function with a tuple-list as an argument but I keep getting the error: "unresolved flex record (need to know the names of ALL the fields in this context)" My code is: fun convert d = ( (map (#1) d) , (map (#2) d) ); This is basicaly trying to convert a list of pairs into a pair of lists.I've also tried to declare the type of d as :('a * 'b) list but that resulted in even more errors. I assume that this has something to do with the unknown size of the tupple and

match tuple with null

匆匆过客 提交于 2019-12-22 05:41:20
问题 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. 回答1: 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

patterns for “symmetric” functions

自闭症网瘾萝莉.ら 提交于 2019-12-22 04:49:17
问题 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

Tuple bang patterns

可紊 提交于 2019-12-22 04:46:11
问题 I understand that in: f x = x + 1 where !y = undefined the meaning of the bang pattern is that y is to be evaluated before f . Similarly: f x = x + 1 where !(!a, !b) = (undefined, undefined) the meaning is the same, w.r.t x and y . But what do the bang patterns mean in: f x = x + 1 where (!a, !b) = (undefined, undefined) It doesn't seem to cause undefined to be evaluated. When do in-tuple bang patterns come into effect? If the pattern's tuple is forced? Can anyone give an example where (!a,

How to pattern match head and tail types of a scala list?

混江龙づ霸主 提交于 2019-12-22 04:45:19
问题 I would like to pattern match on different segments of a list in scala on the types of the head and tail : class Solution07 extends FlatSpec with ShouldMatchers { "plain recursive flatten" should "flatten a list" in { val list1 = List(List(1, 1), 2, List(3, List(5, 8))) val list1Flattened = List(1, 1, 2, 3, 5, 8) flattenRecur(list1) should be (list1Flattened) } def flattenRecur(ls: List[Any]): List[Int] = ls match { case (head: Int) :: (tail: List[Any]) => head :: flattenRecur(tail) case

Given mixed accented and normal characters in string not working in java when searching

限于喜欢 提交于 2019-12-22 04:09:16
问题 String text = "Cámélan discovered ônte red aleŕt \n Como se extingue la deuda"; If I give the input Ca, it should highlight from the given string Cá but it's not highlighting. Below is what I tried. Pattern mPattern; String filterTerm; //this is the input which I give from input filter. Say for eg: Ca String regex = createFilterRegex(filterTerm); mPattern = Pattern.compile(regex); private String createFilterRegex(String filterTerm) { filterTerm = Normalizer.normalize(filterTerm, Normalizer

A more complex version of “How can I tell if a string repeats itself in Python?”

徘徊边缘 提交于 2019-12-22 03:45:16
问题 I was reading this post and I wonder if someone can find the way to catch repetitive motifs into a more complex string. For example, find all the repetitive motifs in string = 'AAACACGTACGTAATTCCGTGTGTCCCCTATACGTATACGTTT' Here the repetitive motifs: 'AAAC ACGTACGT AATTCC GTGTGT CCCC TATACGTATACG TTT' So, the output should be something like this: output = {'ACGT': {'repeat': 2, 'region': (5,13)}, 'GT': {'repeat': 3, 'region': (19,24)}, 'TATACG': {'repeat': 2, 'region': (29,40)}} This example