pattern-matching

How to match scala generic type?

℡╲_俬逩灬. 提交于 2019-12-06 04:01:11
问题 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

bash script to check file name begins with expected string

为君一笑 提交于 2019-12-06 02:32:46
问题 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

How can I code this problem? (C++)

风格不统一 提交于 2019-12-06 01:54:00
I am writing a simple game which stores datasets in a 2D grid (like a chess board). Each cell in the grid may contain a single integer (0 means the cell is empty). If the cell contains a number > 0, it is said to be "filled". The set of "filled" cells on the grid is known as a "configuration". My problems is being able to "recognize" a particular configuration, regardless of where the configuration of cells are in the MxN grid. The problem (in my mind), breaks down into the following 2 sub problems: Somehow "normalising" the position of a configuration (for e.g. "rebasing" its position to (0,0

android.util.Patterns.EMAIL_ADDRESS is validating invalid emails

痴心易碎 提交于 2019-12-06 00:12:19
These are few emails which are not valid. email@domain.web .email@domain.com I have checked above emails in following websites, All of those returns invalid. http://isemail.info/about http://sqa.fyicenter.com/Online_Test_Tools/Email_Address_Format_Validator.php . Yet, android.util.Patterns.EMAIL_ADDRESS pattern validates both. Is there a bug or am I missing something? Both seems to be valid email addresses email@domain.web .email@domain.com since any email address contains three components <username>@<mail-server>.<mail-servertype or server-location> Here android.util.Patterns.EMAIL_ADDRESS

Count overlapping regex matches once again

倾然丶 夕夏残阳落幕 提交于 2019-12-05 23:46:12
问题 How can I obtain the number of overlapping regex matches using Python? I've read and tried the suggestions from this, that and a few other questions, but found none that would work for my scenario. Here it is: input example string: akka search pattern: a.*k A proper function should yield 2 as the number of matches, since there are two possible end positions ( k letters). The pattern might also be more complicated, for example a.*k.*a should also be matched twice in akka (since there are two k

Cut string after first occurrence of a character

邮差的信 提交于 2019-12-05 23:20:54
问题 I have strings like 'keepme:cutme' or 'string-without-separator' which should become respectively 'keepme' and 'string-without-separator'. Can this be done in PostgreSQL? I tried: select substring('first:last' from '.+:') But this leaves the : in and won't work if there is no : in the string. 回答1: Use split_part(): SELECT split_part('first:last', ':', 1) AS first_part Returns the whole string if the delimiter is not there. And it's simple to get the 2nd or 3rd part etc. Substantially faster

How can I remove duplicates from a list in Scala with pattern matching?

眉间皱痕 提交于 2019-12-05 21:25:40
As homework i have to write a function that will remove duplicates from a list. It should be recursive and with pattern matching. I am not allowed to use list functions like head,tail,contains,etc... . For sorted lists i came up with this solution: def remove(u:List[Int]):List[Int] = { u match { case Nil => u case hd::hd2::tl => if(hd == hd2) remove(hd2::tl) else hd :: remove(hd2::tl) case hd::tl => hd :: remove(tl) } } How can i do it for unsorted lists? I won't do your homework for you, but hope, this will help. You want to make your function tail-recursive . That means that the recursive

PHP way to execute SQL LIKE matching without a database query?

大兔子大兔子 提交于 2019-12-05 21:05:18
I want to match an input string to my PHP page the same way a match done by the LIKE command in SQL (MySQL) for consistency of other searches. Since (I have seen but don't comprehend) some of the PHP syntax includes SQL commands I am wondering if this is possible? The reason for this is I am now implementing a search of a keyword versus fields in the DB that are stored in a serialized array, which I have to unserialize in PHP and search depending on the structure of the array. I can't query against the table, just need the matching ability of the query. Otherwise I need to find an alternate

Swift Pattern match on Array<Any>

血红的双手。 提交于 2019-12-05 20:44:01
Swift 1.2 I'm trying to pattern match in a switch case in a function that take a type Any as it's parameter, in order to dispatch to a private more specialize init. Here is a Playground extrapolation : import Foundation struct myStruct { } func switchOnAny(any: Any) -> String { println("Dynamic Type == \(any.dynamicType)") switch any { case let array as [Any]: return "Array" case let array as NSArray: return "NSArray" default: return "Default" } } let emptyStringArray : [String] = [] let stringArray : [String] = ["Bob", "Roger"] let intArray = [1, 2, 3] let customStructArray : [myStruct] = []

UTL_MATCH-like function to work with CLOB

泄露秘密 提交于 2019-12-05 20:37:49
My question is: Is there a UTL_MATCH -like function which works with a CLOB rather than a VARCHAR2 ? My specific problem is: I'm on an Oracle database. I have a bunch of pre-written queries which interface with Domo CenterView . The queries have variables in them defined by ${variableName} . I need to rewrite these queries. I didn't write the original so instead of figuring out what a good value for the variables should be I want to run the queries with the application and get what the query was from V$SQL . So my solution is: Do a UTL_MATCH on the queries with the variable stuff in it and V