pattern-matching

pattern matching of the form: Option{..} <-

狂风中的少年 提交于 2019-12-23 07:21:26
问题 What is this form of pattern matching called: Option{..} <- ... , e.g. as it is used here: data Option = Option { cabal :: Maybe String , noStylish :: Bool } ... main = do Option{..} <- cmdArgs defOption cabp <- case cabal of Nothing -> do ... It seems to redefine cabal and nostylish . Before the pattern match cabal has type Option -> Maybe String but after it has type Maybe String . This example comes from the recently uploaded package cabal2ghci . 回答1: This is a GHC syntactic extension

pattern matching of the form: Option{..} <-

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 07:21:13
问题 What is this form of pattern matching called: Option{..} <- ... , e.g. as it is used here: data Option = Option { cabal :: Maybe String , noStylish :: Bool } ... main = do Option{..} <- cmdArgs defOption cabp <- case cabal of Nothing -> do ... It seems to redefine cabal and nostylish . Before the pattern match cabal has type Option -> Maybe String but after it has type Maybe String . This example comes from the recently uploaded package cabal2ghci . 回答1: This is a GHC syntactic extension

Is using pattern names starting with “_” (underscore) for ignored results documented/encouraged/portable?

感情迁移 提交于 2019-12-23 06:57:09
问题 Let's say I want to fork a thread within a do -notation block, but I don't care about the thread id. If I write forkIO action GHC issues warning Warning: A do -notation statement discarded a result of type ThreadId . Suppress this warning by saying _ <- forkOS action This is a good idea as I want to show that the program is discarding some result. However, this way it won't be apparent what's being discarded. I could write threadId <- forkIO action but then we're hiding the fact that we won't

How to use regexp on the results of a sub query?

拟墨画扇 提交于 2019-12-23 02:44:09
问题 I have two tables. User which has id and phone number id phone_no 1 ---- 9912678 2 ---- 9912323 3 ---- 9912366 Admission Table , which has id phone number id phone_no 6 --- 991267823 7 --- 991236621 8 --- 435443455 9 --- 243344333 I want to find all the phone number of Admission's table which has same pattern as users table and update it in users table. So i am trying this select phone_no from admission where phone_no REGEXP (SELECT phone_no FROM `users` AS user WHERE user.phone_no REGEXP '^

Referencing / dereferencing a vector element in a for loop

孤者浪人 提交于 2019-12-22 18:24:14
问题 In the code below, I want to retain number_list , after iterating over it, since the .into_iter() that for uses by default will consume. Thus, I am assuming that n: &i32 and I can get the value of n by dereferencing. fn main() { let number_list = vec![24, 34, 100, 65]; let mut largest = number_list[0]; for n in &number_list { if *n > largest { largest = *n; } } println!("{}", largest); } It was revealed to me that instead of this, we can use &n as a 'pattern': fn main() { let number_list =

Pattern match of scala list with generics [duplicate]

戏子无情 提交于 2019-12-22 18:14:18
问题 This question already has an answer here : Pattern matching on generic type in Scala (1 answer) Closed last year . I have a class case class MyClass[T](values: List[T]) and I'm trying to create a function which will return a value based on the type of T def myFunc: T = values match { case v: List[Boolean] => false case v: List[MyType] => MyType.defaultVal case _ => throw new Exception("unsupported type") } However, I get compilation errors: Expression of type Boolean doesn't conform to

switch statement for an std::pair?

僤鯓⒐⒋嵵緔 提交于 2019-12-22 18:03:21
问题 I want to switch over possible values of two integers, or in another case two bools. For the sake of discussion, suppose I've done auto mypair = std::make_pair(foo, bar); How can I achieve the equivalent of switch(mypair) { case make_pair(true, false): cout << "true and false"; break; case make_pair(false, true) cout << "false and true"; break; case default: cout << "something else"; } with C++11? (C++14/17 also relevant if that helps)? 回答1: C++'s switch statement doesn't have the pattern

Regex to replace a repeating string pattern

我与影子孤独终老i 提交于 2019-12-22 16:13:10
问题 I need to replace a repeated pattern within a word with each basic construct unit. For example I have the string "TATATATA" and I want to replace it with "TA". Also I would probably replace more than 2 repetitions to avoid replacing normal words. I am trying to do it in Java with replaceAll method. 回答1: I think you want this (works for any length of the repeated string): String result = source.replaceAll("(.+)\\1+", "$1") Or alternatively, to prioritize shorter matches: String result = source

UTL_MATCH-like function to work with CLOB

纵然是瞬间 提交于 2019-12-22 10:44:56
问题 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

Swift Pattern match on Array<Any>

泪湿孤枕 提交于 2019-12-22 10:15:19
问题 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