How can I pattern match on a range in Scala?
Inside pattern match it can be expressed with guards: n match { case it if 0 until 5 contains it => “less than five” case it if 5 until 10 contains it => “less than ten” case _ => “a lot” }
Inside pattern match it can be expressed with guards: n match { case it if 0 until 5 contains it => “less than five” case it if 5 until 10 contains it => “less than ten” case _ => “a lot” }
Axiom K is also called the principle of uniqueness of identity proofs, and it is an axiom about the nature of the identity type in Martin-Löf’s dependent type theory. This type doesn’t exist (and in fact cannot be defined) in simpler type theories such as System F, so that is probably the reason why you … Read more
The “virtualized” pattern matcher is a rewrite of the existing matcher. The motivation for doing this was to support virtualization of pattern matching for the polymorphic embedded DSLs, not relevant for 2.10. As Iulian says in the comments below: It’s very similar to how for-comprehensions are compiled: instead of directly generating code, they are translated … Read more
TL;DR As of Python 3.10.0 (alpha6 released March 30, 2021), Python has an official syntactical equivalent called match. The basic syntax is: match value: case condition: action(s) … For older Python versions, there are only workarounds if you don’t want to resort to if–elif–else. See this excellent community post for a collection of some. Example … Read more
Use matches!, e.g.: matches!(my_struct.my_enum, Unknown) Alternatively, you can use PartialEq trait, for example, by #[derive]: #[derive(PartialEq)] enum MyEnum { … } Then your “ideal” variant will work as is. However, this requires that MyEnum‘s contents also implement PartialEq, which is not always possible/wanted.
Like @a_horse commented, you would have to use the regular expression operator ~ to use bracket expressions. But there’s more. I suggest: SELECT * FROM tbl WHERE value ~ ‘^00[^0]’ ^ … match at start of string (your original expression could match at any position). [^0] … a bracket expression (character class) matching any character … Read more
Without going into details and simplifying a bit: For regular parameters apply constructs and unapply de-structures: object S { def apply(a: A):S = … // makes a S from an A def unapply(s: S): Option[A] = … // retrieve the A from the S } val s = S(a) s match { case S(a) => … Read more
You can pattern match all described cases like this: def func(a: Int, b: Int, c: Double) = (a, b, c) match { case (a, b, 0) => b – a case (a, b, c) if c > 9 || a == b => 0 case _ => 1 // add your logic here }
I don’t know if this is appropriate, but you can also build an object just to match that field, or that set of fields (untested code): object WideLoadActorRef { def unapply(wl: WideLoad): Option[ActorRef] = { Some(wl.d) } } someVal match { case WideLoadActorRef(d) => d ! someMessage } or even object WideLoadBnD { def unapplySeq(wl: … Read more