pattern-matching

Pattern matching on Type in Idris

会有一股神秘感。 提交于 2019-12-23 18:04:17
问题 Probably it's elementary but I don't understand why the following function answers 1 for fnc Nat and also, for fnc Integer , which is not even included as a pattern. fnc : Type -> Integer fnc Bool = 1 fnc Nat = 2 回答1: You can't pattern match on type and you shouldn't. When I compile your code I receive next error: warning - Unreachable case: fnc Nat This was already discussed earlier: Old discussion. Some similar question. Some similar issue on GitHub. UPDATE: Finally found more relevant

Why parse error? Indentation?

牧云@^-^@ 提交于 2019-12-23 17:01:06
问题 I wrote this code: addNums key num = add [] key num where add res a:as b:bs | a == [] = res | otherwise = add res:(a+b) as bs At line 3 the interpreter says: parse error (possibly incorrect indentation) I could not find something wrong, neither with the code nor with the indentation. I put four spaces for each tab. Annotation: Even this does not compile: addNums key num = add [] key num where add res a:as b:bs | a == [] = res | otherwise = add res:(a+b) as bs Line 2: Parse error in pattern:

Constants in Haskell and pattern matching [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-23 15:58:01
问题 This question already has answers here : Haskell - Using a constant in pattern matching (2 answers) Closed 3 years ago . How is it possible to define a macro constant in Haskell? Especially, I would like the following snippet to run without the second pattern match to be overlapped. someconstant :: Int someconstant = 3 f :: Int -> IO () f someconstant = putStrLn "Arg is 3" f _ = putStrLn "Arg is not 3" 回答1: You can define a pattern synonym: {-# LANGUAGE PatternSynonyms #-} pattern

Partial/Full-match value in one RDD to values in another RDD

…衆ロ難τιáo~ 提交于 2019-12-23 12:42:21
问题 I have two RDDs where the first RDD has records of the form RDD1 = (1, 2017-2-13,"ABX-3354 gsfette" 2, 2017-3-18,"TYET-3423 asdsad" 3, 2017-2-09,"TYET-3423 rewriu" 4, 2017-2-13,"ABX-3354 42324" 5, 2017-4-01,"TYET-3423 aerr") and the second RDD has records of the form RDD2 = ('mfr1',"ABX-3354") ('mfr2',"TYET-3423") I need to find all the records in RDD1 which have a full match/partial match for each value in RDD2 matching the 3rd Column of RDD1 to 2nd column of RDD2 and get the count For this

Matching a stream for certain conditions

只谈情不闲聊 提交于 2019-12-23 10:47:43
问题 I am looking for a Java library that allows to match a sequence of objects, potentially mixing with matchers such as those of hamcrest. Ideally I would like to write a test that can check that an iterable contains a sequence that would look like a regular expression, but for objects rather than character strings: assertThat(myList).inSequence(oneOrMore(any()),zeroOrMore(equals(MyObject))); Mockito with verify is close what I would like, but some simple matchers are missing (like zeroOrMore)

Pattern matching zero-argument functions in scala: mystified by warning

删除回忆录丶 提交于 2019-12-23 10:28:15
问题 I'm playing with scala's distributed actors. Very nice. I have a server which executes incoming function objects. For example, the client has object Tasks { def foo = {Console.println("I am Foo")}; def bar = {Console.println("I am Bar");} } // In client actor... ... server ! Tasks.foo _ ... And the server can pick these up and execute them with actor code like react { case task:(()=>Unit) => task() This all works nicely (which is very very cool indeed) but I'm mystified by a warning message

Trie implementation with wildcard values

限于喜欢 提交于 2019-12-23 10:24:15
问题 I'm implementing an algorithm to do directory matching. So I'm given a set of valid paths that can include wildcards (denoted by "X"). Then when I pass in an input I need to know if that input matches with one of the paths in my valid set. I'm running into a problem with the wildcards when a wildcard value that is passed in actually matches with another valid value. Here is an example: Set of valid paths: /guest /guest/X /X/guest /member /member/friends /member/X /member/X/friends Example

Convert match statement to partial function when foreach is used

青春壹個敷衍的年華 提交于 2019-12-23 09:48:32
问题 IntelliJ gives me a hint on a following code: val l = List(0, "1", 2, "3") l.foreach{_ match {case xx:Int => println(xx);case _ =>}} The hint is "Convert match statement to partial function" When I change the foreach to l.foreach{case x:Int => println(x)} I get the scala.MatchError exception. I can use collect instead of foreach , however that produces a resulting List which is never used. Is there some common way how to handle this (something like foreach ignoring the non-matched values), or

Alternatives to matching floating points

余生长醉 提交于 2019-12-23 08:57:53
问题 Rust has decided to disallow float literals in patterns: Matching on floating-point literal values is totally allowed and shouldn't be #41255. It is currently a warning but will be a hard error in a future release. My question is then, how do I achieve the equivalent for example with the following code?: struct Point { x: f64, y: f64, } let point = Point {x: 5.0, y: 4.0}; match point { Point {x: 5.0 , y} => println!("y is {} when x is 5", y), // Causes warning _ => println!("x is not 5") } Is

Preventing move semantics during pattern matching

▼魔方 西西 提交于 2019-12-23 08:49:09
问题 I have a silly example here, just to demonstrate an issue I'm running into with another library and pattern matching. struct Person { name: String, age: i32, choice: Choices } #[derive(Debug)] enum Choices { Good, Neutral, Evil } fn find(p: Person) { match (p.choice, p.age) { (Choices::Good, a) if a < 80 => { announce(p); } (_, a) if a >= 80 => { println!("You're too old to care."); } _ => { println!("You're not very nice!") } } } fn announce(p: Person) { println!("Your name is {}. You are {: