pattern-matching

Make Compile Fail on Non-Exhaustive Match in SBT

喜你入骨 提交于 2019-12-05 12:38:41
问题 Let's say that I have a trait, Parent, with one child, Child. scala> sealed trait Parent defined trait Parent scala> case object Boy extends Parent defined module Boy I write a function that pattern matches on the sealed trait. My f function is total since there's only a single Parent instance. scala> def f(p: Parent): Boolean = p match { | case Boy => true | } f: (p: Parent)Boolean Then, 2 months later, I decide to add a Girl child of Parent . scala> case object Girl extends Parent defined

Google Sheets Pattern Matching/RegEx for COUNTIF

家住魔仙堡 提交于 2019-12-05 12:35:06
问题 The documentation for pattern matching for Google Sheets has not been helpful. I've been reading and searching for a while now and can't find this particular issue. Maybe I'm having a hard time finding the correct terms to search for but here is the problem: I have several numbers (part numbers) that follow this format: ##-#### Categories can be defined by the part numbers, i.e. 50-03## would be one product category, and the remaining 2 digits are specific for a model. I've been trying to run

Scala lists with existential types: `map{ case t => … }` works, `map{ t => … }` doesn't?

半城伤御伤魂 提交于 2019-12-05 12:13:58
Suppose that we have defined an existential type: type T = (X => X, X) forSome { type X } and then defined a list of type List[T] : val list = List[T]( ((x: Int) => x * x, 42), ((_: String).toUpperCase, "foo") ) It is well known [ 1 ], [ 2 ] that the following attempt to map does not work: list.map{ x => x._1(x._2) } But then, why does the following work?: list.map{ case x => x._1(x._2) } Note that answers to both linked questions assumed that a type variable is required in the pattern matching, but it also works without the type variable. The emphasis of the question is more on Why does the {

Normalizing by max value or by total value?

守給你的承諾、 提交于 2019-12-05 12:12:25
I'm doing some work that involves document comparison. To do this, I'm analizing each document, and basically counting the number of times some key words appear on each of these documents. For instance: Document 1: Document 2: Book -> 3 Book -> 9 Work -> 0 Work -> 2 Dollar -> 5 Dollar -> 1 City -> 18 City -> 6 So after the counting process, I store all these sequence of numbers in a vector. This sequence of numbers will represent the feature vector for each document. Document 1: [ 3, 0, 5, 18] Document 2: [ 9, 2, 1, 6] The final step would be to normalize the data in a range from [0 1] . But

Match the nth longest possible string in Perl

╄→гoц情女王★ 提交于 2019-12-05 11:23:37
The pattern matching quantifiers of a Perl regular expression are "greedy" (they match the longest possible string). To force the match to be "ungreedy", a ? can be appended to the pattern quantifier (*, +). Here is an example: #!/usr/bin/perl $string="111s11111s"; #-- greedy match $string =~ /^(.*)s/; print "$1\n"; # prints 111s11111 #-- ungreedy match $string =~ /^(.*?)s/; print "$1\n"; # prints 111 But how one can find the second, third and .. possible string match in Perl? Make a simple example of yours --if need a better one. Miller Utilize a conditional expression , a code expression ,

Create new guard clause

雨燕双飞 提交于 2019-12-05 11:02:47
问题 In Elixir, how would I go about creating a new guard clause for a function? Obviously, I've seen that you can't just call any function in a when statement, but it would be nice to be able to do something like this: defmodule Player do def play_card(player), do: [] def play_card(player) when has_cards(player), do: ... # Define has_cards as guard clause? end 回答1: You can use Kernel.defguard/1 (or defguardp/1 ) to do this now: defmodule Player do defstruct [:name, cards: []] defguard has_cards

Monoid mempty in pattern matching

╄→尐↘猪︶ㄣ 提交于 2019-12-05 10:51:31
I tried to write a generalized maximum function similar to the one in Prelude . My first naiv approach looked like this: maximum' :: (F.Foldable a, Ord b) => a b -> Maybe b maximum' mempty = Nothing maximum' xs = Just $ F.foldl1 max xs However, when I test it it always returns Nothing regardless of the input: > maximum' [1,2,3] > Nothing Now I wonder whether it's possible to obtain the empty value of a Monoid type instance. A test function I wrote works correctly: getMempty :: (Monoid a) => a -> a getMempty _ = mempty > getMempty [1,2,3] > [] I had already a look at these two questions but I

What is syntax for byte_size in pattern matching?

喜欢而已 提交于 2019-12-05 10:49:48
How to match and what syntax is to check byte_size equal 12 length pattern in handle_info() ? Can I use both patterns in handle_info() , eg. first that will check string for new line, second with byte_size ? Example code without byte_size pattern: def init(_) do {:ok, []} end def handle_info({:elixir_serial, serial, "\n"}, state) do {:noreply, Enum.reverse(state)} end def handle_info({:elixir_serial, serial, data}, state) do Logger.debug "data: #{data}" {:noreply, [data | state]} end Yes, you can use both patterns, this is the purpose of having multiple function clauses. From top to bottom,

Why do I get an error when pattern matching a struct-like enum variant with fields?

和自甴很熟 提交于 2019-12-05 10:38:13
I can't get rid of an error on this code: #[derive(PartialEq, Copy, Clone)] pub enum OperationMode { ECB, CBC { iv: [u8; 16] }, } pub struct AES { key: Vec<u8>, nr: u8, mode: OperationMode, } impl AES { pub fn decrypt(&mut self, input: &Vec<u8>) { match self.mode { OperationMode::ECB => {}, OperationMode::CBC(_) => {}, }; } } The pattern matching at the end of the decrypt function gives an error: error[E0532]: expected tuple struct/variant, found struct variant `OperationMode::CBC` --> src/main.rs:17:13 | 17 | OperationMode::CBC(_) => {}, | ^^^^^^^^^^^^^^^^^^ did you mean `OperationMode::CBC {

Pattern Matching of Units of Measure in F#

半世苍凉 提交于 2019-12-05 09:42:05
This function: let convert (v: float<_>) = match v with | :? float<m> -> v / 0.1<m> | :? float<m/s> -> v / 0.2<m/s> | _ -> failwith "unknown" produces an error The type 'float<'u>' does not have any proper subtypes and cannot be used as the source of a type test or runtime coercion. Is there any way how to pattern match units of measure? As @kvb explains in detail, the problem is that units of measure are a part of the type. This means that float<m> is different type than float<m/s> (and unfortunately, this information isn't stored as part of the value at runtime). So, you're actually trying