pattern-matching

How can I code this problem? (C++)

狂风中的少年 提交于 2019-12-07 13:04:38
问题 I am writing a simple game which stores datasets in a 2D grid (like a chess board). Each cell in the grid may contain a single integer (0 means the cell is empty). If the cell contains a number > 0, it is said to be "filled". The set of "filled" cells on the grid is known as a "configuration". My problems is being able to "recognize" a particular configuration, regardless of where the configuration of cells are in the MxN grid. The problem (in my mind), breaks down into the following 2 sub

In Scala, how to test the type of an 'Any' object against a type with type parameter?

大憨熊 提交于 2019-12-07 12:18:24
问题 I am trying to get a type-safe way of converting the result of parsing a JSON string. I want to check whether a field is Map[String, any] or a plain string. My first attempt is def test(x:Any) = { x match { case m:Map[String,Any] => ... ... } This causes "non-variable type argument String in type pattern Map[String,Any] is unchecked since it is eliminated by erasure" Looking through the document of TypeTag and ClassTag, I could not find a good way to accomplish that. The following code does

PHP way to execute SQL LIKE matching without a database query?

て烟熏妆下的殇ゞ 提交于 2019-12-07 12:08:32
问题 I want to match an input string to my PHP page the same way a match done by the LIKE command in SQL (MySQL) for consistency of other searches. Since (I have seen but don't comprehend) some of the PHP syntax includes SQL commands I am wondering if this is possible? The reason for this is I am now implementing a search of a keyword versus fields in the DB that are stored in a serialized array, which I have to unserialize in PHP and search depending on the structure of the array. I can't query

Where Clause Applied To Multiple Patterns

梦想的初衷 提交于 2019-12-07 11:11:04
问题 I have a function with multiple patterns. I have two or more of them which share the same expression which I want to replace. Now if I write a where clause at the bottom, indent it and define a new variable as the expression I wanted to replace it won't work. Example: myFunction firstParam secondParam = expression myFunction firstParam _ = 1 + expression where expression = firstParam + secondParam Compiler message: Not in scope: `expression' Not in scope: `secondParam' How do I do it? 回答1:

Scala - how to pattern match when chaining two implicit conversions?

陌路散爱 提交于 2019-12-07 10:20:42
问题 I wrote a method to parse Metrics data and at first faced a problem with the type of transactionMap which is a java.util.Map . And I solved it using JavaConverters. def parseMetrics(metric: Metric) = { import scala.collection.JavaConverters._ metric.transactionMap.asScala.values.map { case false => "N" case true => "Y" }.toList But after that I got an error while pattern matching true and false values: pattern type is incompatible with expected type, found: Boolean, required: java.lang

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

笑着哭i 提交于 2019-12-07 09:04:39
问题 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,

Monoid mempty in pattern matching

和自甴很熟 提交于 2019-12-07 08:08:51
问题 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

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

余生颓废 提交于 2019-12-07 07:18:08
问题 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

Normalizing by max value or by total value?

﹥>﹥吖頭↗ 提交于 2019-12-07 07:10:34
问题 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

What is syntax for byte_size in pattern matching?

≯℡__Kan透↙ 提交于 2019-12-07 05:59:46
问题 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 回答1: Yes