pattern-matching

number_in_month exercise (Error in SML function to build a list of integers from a list of tuples)

纵然是瞬间 提交于 2020-03-23 09:03:28
问题 val test1 = [(1,5,3),(3,5,2),(3,4,5)] fun number_in_month dates_and_month = case dates_and_month of (x,y,z)::xs' => y :: number_in_month xs' This code produces the following error when I run in the REPL with test1: uncaught exception Match [nonexhaustive match failure] raised at: hw1pm.sml:28.49 Any clue why? 回答1: It did not know what do when the list was empty. Working code: fun number_in_month dates_and_month = case dates_and_month of [] => [] | (x,y,z)::xs' => y :: number_in_month xs' 回答2:

how does scala's type check work in this case?

最后都变了- 提交于 2020-03-17 09:19:50
问题 // Start writing your ScalaFiddle code here sealed trait DSL[A]{ // def run(): A ={ // this match { // case GetLength(something) => // something.length // case ShowResult(number) => // s"the length is $number" // } // } } case class GetLength(something: String) extends DSL[Int] case class ShowResult(number: Int) extends DSL[String] def run[A](fa:DSL[A]): A ={ fa match { case GetLength(something) => something.length case ShowResult(number) => s"the length is $number" } } val dslGetLength =

Is there a simple way to mutate an enum field in Rust?

白昼怎懂夜的黑 提交于 2020-03-16 06:05:04
问题 Suppose we have an enum that looks like this: enum MyEnum { Field1, Field2 {x: f64, y: f64}, /* Maybe some other fields */ MyString(String), } Now I created an instance of this enum of the subtype MyString and after some actions I want to mutate it. For example: fn main() { let mut my_enum = MyEnum::MyString("Hello, world".to_string()); /* Some actions */ // Mutating the string match my_enum { MyEnum::MyString(ref mut content) => { content.push('!'); }, _ => {} } // Printing the string match

Is there a simple way to mutate an enum field in Rust?

若如初见. 提交于 2020-03-16 06:02:13
问题 Suppose we have an enum that looks like this: enum MyEnum { Field1, Field2 {x: f64, y: f64}, /* Maybe some other fields */ MyString(String), } Now I created an instance of this enum of the subtype MyString and after some actions I want to mutate it. For example: fn main() { let mut my_enum = MyEnum::MyString("Hello, world".to_string()); /* Some actions */ // Mutating the string match my_enum { MyEnum::MyString(ref mut content) => { content.push('!'); }, _ => {} } // Printing the string match

Get all values before a decimal number / an integer from a list of strings in Python

给你一囗甜甜゛ 提交于 2020-03-04 05:05:10
问题 I have a list of strings and I want to split each string on a floating point number. If there is no floating point number in the string, I want to split it on a number. It should only split once and return everything before and after it separated by commas. Input string: ['Naproxen 500 Active ingredient Ph Eur', 'Croscarmellose sodium 22.0 mg Disintegrant Ph Eur', 'Povidone K90 11.0 Binder 56 Ph Eur', 'Water, purifieda, 'Silica, colloidal anhydrous 2.62 Glidant Ph Eur', 'Water purified 49

RegEx To Ignore Text Between Quotes

China☆狼群 提交于 2020-02-24 05:17:48
问题 I have a Regex, which is [\\.|\\;|\\?|\\!][\\s] This is used to split a string. But I don't want it to split . ; ? ! if it is in quotes. 回答1: I'd not use split but Pattern & Matcher instead. A demo: import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String text = "start. \"in quotes!\"; foo? \"more \\\" words\"; bar"; String simpleToken = "[^.;?!\\s\"]+"; String quotedToken = "(?x) # enable inline comments and ignore

Test if exact string appears in a Pandas Series

有些话、适合烂在心里 提交于 2020-02-15 08:35:32
问题 In a Pandas df['Column'] (i.e. a pandas Series) If I use df['company_name'].str.contains('ABC').any() I will get 'True' if an entry is "ABC" But it will also return a (false positive) "True" if some other entry in the Series is "ABC PTY LTD" I only want to match if there is an entry that is exactly "ABC" I've checked about 50 similar questions but none answer this one. I tried a Regex rec_df['recruiters'].str.match( r'^ABC$').any() It works but the problem is I want to pass the 'ABC' part

Test if exact string appears in a Pandas Series

假如想象 提交于 2020-02-15 08:34:11
问题 In a Pandas df['Column'] (i.e. a pandas Series) If I use df['company_name'].str.contains('ABC').any() I will get 'True' if an entry is "ABC" But it will also return a (false positive) "True" if some other entry in the Series is "ABC PTY LTD" I only want to match if there is an entry that is exactly "ABC" I've checked about 50 similar questions but none answer this one. I tried a Regex rec_df['recruiters'].str.match( r'^ABC$').any() It works but the problem is I want to pass the 'ABC' part

What is the purpose of `&` before the loop variable?

点点圈 提交于 2020-02-11 08:40:21
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It

What is the purpose of `&` before the loop variable?

谁都会走 提交于 2020-02-11 08:40:20
问题 What is the purpose of & in the code &i in list ? If I remove the & , it produces an error in largest = i , since they have mismatched types (where i is &32 and i is i32 ). But how does &i convert i into i32 ? fn largest(list: &[i32]) -> i32 { println!("{:?}", list); let mut largest = list[0]; for &i in list { if i > largest { largest = i; } } largest } fn main() { let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32]; println!("The largest number is: {}", largest(&hey)); } Playground It