pattern-matching

Does Haskell provide an idiom for pattern matching against many possible data constructors?

跟風遠走 提交于 2020-05-08 14:28:01
问题 Working on a Haskell project, I'm dealing with the Event data type from the FSNotify package. The constructors for Event are all: Added FilePath UTCTime Modified FilePath UTCTime Removed FilePath UTCTime In my code, I'm only interested in extracting the FilePath from the Event and doing the same action regardless of the type constructor; because of this, I'm tempted to make a lambda. Unfortunately, the code suffers reduced readability when I drop a case expression into the lambda to pattern

Grok pattern to match email address

倾然丶 夕夏残阳落幕 提交于 2020-04-30 07:29:10
问题 I have the following Grok patterns defined in a pattern file HOSTNAME \b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b) EMAILLOCALPART [a-zA-Z][a-zA-Z0-9_.+-=:]+ EMAILADDRESS %{EMAILLOCALPART}@%{HOSTNAME} For some reason this doesn't compile when run against http://grokdebug.herokuapp.com/ with the following input, it simply returns "Compile error" Node1\Spam.log.2016-05-03 171 1540699703 03/May/2016 00:00:01 +0000 INFO [http-bio-0.0.0.0-8001-exec-20429]

Grok pattern to match email address

北慕城南 提交于 2020-04-30 07:29:07
问题 I have the following Grok patterns defined in a pattern file HOSTNAME \b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b) EMAILLOCALPART [a-zA-Z][a-zA-Z0-9_.+-=:]+ EMAILADDRESS %{EMAILLOCALPART}@%{HOSTNAME} For some reason this doesn't compile when run against http://grokdebug.herokuapp.com/ with the following input, it simply returns "Compile error" Node1\Spam.log.2016-05-03 171 1540699703 03/May/2016 00:00:01 +0000 INFO [http-bio-0.0.0.0-8001-exec-20429]

Grep from a range of dates as filenames

ぐ巨炮叔叔 提交于 2020-04-18 12:41:29
问题 I have multiple files which have raw CDRs in them. The files are named in the pattern cdr.log-2013-06-08-0100 . There are multiple files like so: cdr.log-2013-06-07-0000 cdr.log-2013-06-07-0100 . . cdr.log-2013-06-08-2200 cdr.log-2013-06-08-2300 I need to grep out a string from a range of files say between the date 6th june 2013 to 9th june 2013, how can I accomplish this? 回答1: To grep a string from specific range of file. find . -regex ".*cdr\.log-2013-06-0[6-9]-.*" -exec grep 'your string'

OCaml Typechecking Problem With Functors and Polymorphic Variants

限于喜欢 提交于 2020-04-17 22:34:08
问题 I have a problem using functors in OCaml. I have a module type TASK that is used to have different kinds of tasks: module type TASK = sig type task_information type task_information_as_lists type abstract_u_set_type module AbstractUSet : Set.S with type t = abstract_u_set_type val mk_task_information : task_information_as_lists -> task_information end A module of type TASK will contain algorithms that use nodes. These nodes will have different types. I therefore built TASK_NODE : module type

C# 8 switch expression: Handle multiple cases at once?

℡╲_俬逩灬. 提交于 2020-04-16 19:27:10
问题 C# 8 introduced pattern matching, and I already found good places to use it, like this one: private static GameType UpdateGameType(GameType gameType) { switch (gameType) { case GameType.RoyalBattleLegacy: case GameType.RoyalBattleNew: return GameType.RoyalBattle; case GameType.FfaLegacy: case GameType.FfaNew: return GameType.Ffa; default: return gameType; } } which then becomes private static GameType UpdateGameType(GameType gameType) => gameType switch { GameType.RoyalBattleLegacy =>

PostgreSQL full text search abbreviations

筅森魡賤 提交于 2020-04-16 11:48:12
问题 I created a Postgresql full text search using 'german'. How can I configer, that when I search for "Bezirk", lines containing "Bez." are also a match? (And vice-versa) 回答1: @pozs is right. You need to use a synonym dictionary. 1 - In the directory $SHAREDIR/tsearch_data create the file german.syn with the following contents: Bez Bezirk 2 - Execute the query: CREATE TEXT SEARCH DICTIONARY german_syn ( template = synonym, synonyms = german); CREATE TEXT SEARCH CONFIGURATION german_syn(COPY=

PostgreSQL full text search abbreviations

£可爱£侵袭症+ 提交于 2020-04-16 11:47:40
问题 I created a Postgresql full text search using 'german'. How can I configer, that when I search for "Bezirk", lines containing "Bez." are also a match? (And vice-versa) 回答1: @pozs is right. You need to use a synonym dictionary. 1 - In the directory $SHAREDIR/tsearch_data create the file german.syn with the following contents: Bez Bezirk 2 - Execute the query: CREATE TEXT SEARCH DICTIONARY german_syn ( template = synonym, synonyms = german); CREATE TEXT SEARCH CONFIGURATION german_syn(COPY=

Is it possible to match against the result of a `const fn`?

ぃ、小莉子 提交于 2020-04-07 05:07:46
问题 I've tried the naive approach fn main() -> Result<(), Box<std::error::Error>> { let num = 0; match num { u64::max_value() => println!("Is u64::max_value()"), _ => println!("Is boring") } Ok(()) } but it fails with expected tuple struct/variant, found method <u64>::max_value . Is there another syntax except n if n == u64::max_value() => ... which can I use? 回答1: The left part of => must be a pattern, and few expressions are also valid patterns. A call-expression is not a valid pattern. Named

Assignment from Rust match statement

為{幸葍}努か 提交于 2020-04-06 03:46:28
问题 Is there an idiom in Rust which is used to assign the value of a variable based on a match clause? I know something like val b = a match { case x if x % 2 == 1 => false case _ => true } from Scala and was wondering whether you can do the same in Rust. Is there a way to evaluate a match clause as an expression and return something from it or is it just a statement in Rust? 回答1: In Rust, nearly every statement is also an expression. You can do this: fn main() { let a = 3; let b = match a { x if