pattern-matching

How to detect identical part(s) inside string?

纵然是瞬间 提交于 2019-12-09 14:16:20
问题 I try to break down the decoding algorithm wanted question into smaller questions. This is Part I. Question: two strings: s1 and s2 part of s1 is identical to part of s2 space is separator how to extract the identical part(s)? example 1: s1 = "12 November 2010 - 1 visitor" s2 = "6 July 2010 - 100 visitors" the identical parts are "2010", "-", "1" and "visitor" example 2: s1 = "Welcome, John!" s2 = "Welcome, Peter!" the identical parts are "Welcome," and "!" example 3: (to clarify the "!"

Is there any fundamental limitations that stops Scala from implementing pattern matching over functions?

十年热恋 提交于 2019-12-09 14:06:41
问题 In languages like SML, Erlang and in buch of others we may define functions like this: fun reverse [] = [] | reverse x :: xs = reverse xs @ [x]; I know we can write analog in Scala like this (and I know, there are many flaws in the code below): def reverse[T](lst: List[T]): List[T] = lst match { case Nil => Nil case x :: xs => reverse(xs) ++ List(x) } But I wonder, if we could write former code in Scala, perhaps with desugaring to the latter. Is there any fundamental limitations for such

What's the reasoning behind adding the “case” keyword to Scala?

落花浮王杯 提交于 2019-12-09 09:25:55
问题 Apart from: case class A ... case which is quite useful? Why do we need to use case in match ? Wouldn't: x match { y if y > 0 => y * 2 _ => -1 } ... be much prettier and concise? Or why do we need to use case when a function takes a tuple? Say, we have: val z = List((1, -1), (2, -2), (3, -3)).zipWithIndex Now, isn't: z map { case ((a, b), i) => a + b + i } ... way uglier than just: z map (((a, b), i) => a + b + i) ...? 回答1: The second issue, anonymous functions that avoid the case , is a

Why are if expressions frowned upon in Haskell?

淺唱寂寞╮ 提交于 2019-12-09 07:43:01
问题 This has been a question I've been wondering for a while. if statements are staples in most programming languages (at least then ones I've worked with), but in Haskell it seems like it is quite frowned upon. I understand that for complex situations, Haskell's pattern matching is much cleaner than a bunch of ifs, but is there any real difference? For a simple example, take a homemade version of sum (yes, I know it could just be foldr (+) 0 ): sum :: [Int] -> Int -- separate all the cases out

Cleaner Alternative to Extensive Pattern Matching in Haskell

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 05:04:22
问题 Right now, I have some code that essentially works like this: data Expression = Literal Bool | Variable String | Not Expression | Or Expression Expression | And Expression Expression deriving Eq simplify :: Expression -> Expression simplify (Literal b) = Literal b simplify (Variable s) = Variable s simplify (Not e) = case simplify e of (Literal b) -> Literal (not b) e' -> Not e' simplify (And a b) = case (simplify a, simplify b) of (Literal False, _) -> Literal False (_, Literal False) ->

match with string numbers - unreachable code

久未见 提交于 2019-12-09 03:56:55
问题 New to scala, and can't seem to get my match expression to work. I've read about the differences between how the statement is evaluated (e.g. a new variable as opposed to one declared), but can't seem to get backticks or capitalization to work. // declared inside of object val numberOne = "+17201234567" val numberTwo = "+17201235678" def returnSomething(number: String): String = number match { case numberOne => "my first number" case numberTwo => "my second number" case _ => "a default number

Data structure for large number of patterns

戏子无情 提交于 2019-12-09 03:34:31
问题 In an interview, I was asked to come up with data structure that can hold millions of patterns and enables fast searching through them to find the longest matching one. For instance, patterns are like: 1- 8876 8893 87 | true 2- 8876 889 | false 3- 8876 8 | false 4- 887 | true The input is a number with at least 2 and at most 18 digits and we need to find the longest matching pattern from the data structure and extract the boolean at the end. For example, 8876 8893 9943 53 would match the 1

Working around incomplete pattern matching on enums

为君一笑 提交于 2019-12-09 02:41:46
问题 Are there any creative ways to work around .NET's "weak" enums when pattern matching? I'd like them to function similarly to DUs. Here's how I currently handle it. Any better ideas? [<RequireQualifiedAccess>] module Enum = let unexpected<'a, 'b, 'c when 'a : enum<'b>> (value:'a) : 'c = //' failwithf "Unexpected enum member: %A: %A" typeof<'a> value //' match value with | ConsoleSpecialKey.ControlC -> () | ConsoleSpecialKey.ControlBreak -> () | _ -> Enum.unexpected value //without this, gives

How can I pattern match against an Option<String>?

萝らか妹 提交于 2019-12-09 02:19:35
问题 I can straight-forwardly match a String in Rust: let a = "hello".to_string(); match &a[..] { "hello" => { println!("Matches hello"); } _ => panic!(), } If I have an option type, it fails: match Some(a) { Some("hello") => { println!("Matches some hello"); } _ => panic!(), } because the types don't match: error[E0308]: mismatched types --> src/main.rs:5:14 | 5 | Some("hello") => { | ^^^^^^^ expected struct `std::string::String`, found reference | = note: expected type `std::string::String`

C# 7 switch case with null checks

淺唱寂寞╮ 提交于 2019-12-08 21:46:01
问题 C#7 introduces a new feature called patterns , which you can use with Is-Expression or Switch cases like this: string str = null; switch(str){ case string x: Console.WriteLine("string " + x); break; default: Console.WriteLine("default"); break; } and you would expect that it will goes inside case #1, as it is the same type, but it didn't. 回答1: Despite what you might think, string x = null actually isn't a string at all. It is 'nothing', assigned to a variable of type string. The check in your