pattern-matching

Pattern matching and infinite streams

我只是一个虾纸丫 提交于 2019-12-04 10:01:39
问题 So, I'm working to teach myself Scala, and one of the things I've been playing with is the Stream class. I tried to use a naïve translation of the classic Haskell version of Dijkstra's solution to the Hamming number problem: object LazyHammingBad { private def merge(a: Stream[BigInt], b: Stream[BigInt]): Stream[BigInt] = (a, b) match { case (x #:: xs, y #:: ys) => if (x < y) x #:: merge(xs, b) else if (y < x) y #:: merge(a, ys) else x #:: merge(xs, ys) } val numbers: Stream[BigInt] = 1 #::

Approximate matching of two lists of events (with duration)

可紊 提交于 2019-12-04 09:20:48
I have a black box algorithm that analyses a time series and "detects" certain events in the series. It returns a list of events, each containing a start time and end time. The events do not overlap. I also have a list of the "true" events, again with start time and end time for each event, not overlapping. I want to compare the two lists and match detected and true events that fall within a certain time tolerance (True Positives). The complication is that the algorithm may detect events that are not really there (False Positives) or might miss events that were there (False Negatives). What is

How to match scala generic type?

寵の児 提交于 2019-12-04 09:18:30
Is there any way to match just on generic type passed in function? I'd like to do: def getValue[T](cursor: Cursor, columnName: String): T = { val index = cursor.getColumnIndex(columnName) T match { case String => cursor.getString(index) case Int => cursor.getInteger(index) } I thought about something like classOf or typeOf , but none of them is acceptable for just types, but objects. My idea was also to create some object of type T and then check its type, but I think there can be a better solution. You could use ClassTag . val string = implicitly[ClassTag[String]] def getValue[T : ClassTag] =

Regular expression to match 12345

徘徊边缘 提交于 2019-12-04 08:39:13
Is there a regex to match a string of increasing contiguous numbers, e.g. 123, 56789, etc? I don't think it can be in regex but worth checking with folks here. ^(?:0(?=1|$))?(?:1(?=2|$))?(?:2(?=3|$))?(?:3(?=4|$))?(?:4(?=5|$))?(?:5(?=6|$))?(?:6(?=7|$))?(?:7(?=8|$))?(?:8(?=9|$))?(?:9$)?$ Result: http://rubular.com/r/JfJJ6ntEQG ^(1|^)((2|^)((3|^)((4|^)((5|^)((6|^)((7|^)((8|^)((9|^))?)?)?)?)?)?)?)?$ Python demonstration: >>> import re >>> good = ['1', '12', '123', '23', '3', '4', '45', '456', '56', '6', '7', '78', '789', '89', '9', '123456789'] >>> bad = ['a', '11', '13', '1345', '2459', '321',

Implementing pattern matching in C#

只愿长相守 提交于 2019-12-04 08:22:33
问题 In Scala, you can use pattern matching to produce a result depending on the type of the input. For instance: val title = content match { case blogPost: BlogPost => blogPost.blog.title + ": " + blogPost.title case blog: Blog => blog.title } In C#, I'd ideally like to be able to write: var title = Visit(content, (BlogPost blogPost) => blogPost.Blog.Title + ": " + blogPost.Title, (Blog blog) => blog.Title ); Is this possible? When I've tried writing it as a single method, I don't know how to

Finding patterns in list

社会主义新天地 提交于 2019-12-04 08:19:26
问题 I am currently searching for a way to find patterns in a list of integers, but the method I am going to use would be applicable to strings and other lists with different elements of course. Now let me explain what I am looking for. I want to find the longest repeating pattern in a list of integers. For example, [1, 2, 3, 4, 1, 2, 3] # This list would give 1, 2, 3 Overlapping patterns should be discarded. ( Not certain ) [1, 1, 1, 1, 1] # Should give 1, 1 Not 1, 1, 1, 1 Here is what did not

Scala - case match partial string

瘦欲@ 提交于 2019-12-04 08:00:59
问题 I have the following: serv match { case "chat" => Chat_Server ! Relay_Message(serv) case _ => null } The problem is that sometimes I also pass an additional param on the end of the serv string, so: var serv = "chat.message" Is there a way I can match a part of the string so it still gets sent to Chat_Server? Thanks for any help, much appreciated :) 回答1: Use regexes ;) val Pattern = "(chat.*)".r serv match { case Pattern(chat) => "It's a chat" case _ => "Something else" } And with regexes you

bash script to check file name begins with expected string

↘锁芯ラ 提交于 2019-12-04 07:52:53
Running on OS X with a bash script: sourceFile=`basename $1` shopt -s nocasematch if [[ "$sourceFile" =~ "adUsers.txt" ]]; then echo success ; else echo fail ; fi The above works, but what if the user sources a file called adUsers_new.txt ? I tried: if [[ "$sourceFile" =~ "adUsers*.txt" ]]; then echo success ; else echo fail ; fi But the wildcard doesn't work in this case. I'm writing this script to allow for the user to have different iterations of the source file name, which must begin with aduser and have the .txt file extension. In bash , you can get the first 7 characters of a shell

Replacing ( string ) ^ 2 to sqrt ( string ) in Perl

白昼怎懂夜的黑 提交于 2019-12-04 07:13:23
问题 A given string (called $bbb !) contains many operands and operators. I want to replace every occurrence of muth ( math ) ^ 2 mith to muth sqrt( math ) mith . (whitespace can be more than just one). EDIT: Assume that, in the entire expression, there is only either one (simple linear expression) ^ 2 or none --if it makes it easier. Inclusive Example: 1.2 * ( 4.7 * a * ( b - 0.02 ) ^ 2 * ( b - 0.02 + 1 ) / ( b - 0.0430 ) ) should be changed to: 1.2 * ( 4.7 * a * sqrt( b - 0.02 ) * ( c - 0.02 + 1

How get all matching positions in a string?

我与影子孤独终老i 提交于 2019-12-04 07:13:04
问题 I have a column flag_acumu in a table in PostgreSQL with values like: 'SSNSSNNNNNNNNNNNNNNNNNNNNNNNNNNNNSNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN' I need to show all positions with an 'S'. With this code, I only get the first such position, but not the later ones. SELECT codn_conce, flag_acumu, position('S' IN flag_acumu) AS the_pos FROM dh12 WHERE position('S' IN flag_acumu) != 0 ORDER BY the_pos ASC; How to get all of them? 回答1: Since you didn't specify your needs to a point in which one