guard-clause

Pattern match guard with DateTime.TryParseExact?

百般思念 提交于 2019-12-13 00:41:20
问题 How to guard with DateTime.TryParseExact (and get the parsed value if possible)? The following code doesn't work. [<EntryPoint>] let main args = let argList = args |> List.ofSeq match argList with | "aaa" :: [] -> aaa.main "aaa" | "bbb" :: [] -> bbb.main "bbb" | "ccc" :: yyyymm :: [] when DateTime.TryParseExact (yyyymm, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None)-> ccc.main "ccc" yyyymm 回答1: You can use a mutable : let mutable dt = Unchecked.defaultof<_> match argList with |

Guard Clause Not Firing

若如初见. 提交于 2019-12-11 05:04:56
问题 So I have been trying to get guard clauses to work with Caliburn.Micro and a bound textbox. The View: <TextBox x:Name="UserAccount_DisplayName" Margin="-10,-5,-10,8"/> <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False"> <shell:ApplicationBar.Buttons> <cal:AppBarButton IconUri="\Resources\Iconography\appbar.check.rest.png" Text="Save" Message="SaveAndNavigateToAddAccountView" /> </shell:ApplicationBar.Buttons> </shell:ApplicationBar> <

Pattern combining type test and literal

久未见 提交于 2019-12-11 02:40:15
问题 The active pattern in this question fails to compile after upgrading to VS 2012 RTM. It provides a way to do a type test and match a literal within a single pattern. For example: let (|Value|_|) value = match box value with | :? 'T as x -> Some x | _ -> None let getValue (name: string) (r: IDataReader) = match r.[name] with | null | :? DBNull | Value "" -> Unchecked.defaultof<_> | v -> unbox v Can this be done without the active pattern? I realize a when guard could be used ( :? string as s

Guard clauses in prolog?

拜拜、爱过 提交于 2019-12-08 16:23:12
问题 Do they exist? How are they implemented? The coroutining predicates of SWI-Prolog ( freeze , when , dif etc.) have the functionality of guards. How do they fit in the preferred Prolog programming style? I am very new to logic programming (with Prolog and altogether) and somewhat confused by the fact that it is not purely declarative, and requires procedural considerations even in very simple cases (see this question about using \== or dif). Am I missing something important? 回答1: First a

NameError: undefined - have parsing rules for local variables changed in Ruby 2.1.2?

元气小坏坏 提交于 2019-12-04 03:20:30
问题 I am getting NameError: undefined local variable or method with ruby 2.1.2 As observed in this question, expressions like: bar if bar = true raises an undefined local variable error (provided that bar is not defined prior) because bar is read by the parser before it is assigned. And I believe that there used to be no difference with that with this expression: bar if bar = false The difference between the two is whether the main body is evaluated or not, but that should not matter if

F# pattern match directly against let binding

痴心易碎 提交于 2019-12-01 20:33:49
Is it possible in F# to pattern match directly against a let binding? For example, this compiles without any warnings: let value = match arg with | 1 -> "value1" | 2 -> "value2" | _ -> failwith "key not found" Whereas the following gives the warning "This rule will never be matched" against the lines matching key2 and _ : let key1 = 1 let key2 = 2 let value = match arg with | key1 -> "value1" | key2 -> "value2" | _ -> failwith "key not found" Is this because although they're immutable, the let bindings are unlike C# const variables? just use capital letters and [<Literal>] them and it works as

Pattern matching with guards vs if/else construct in F#

≡放荡痞女 提交于 2019-12-01 03:58:05
In ML-family languages, people tend to prefer pattern matching to if/else construct. In F#, using guards within pattern matching could easily replace if/else in many cases. For example, a simple delete1 function could be rewritten without using if/else (see delete2 ): let rec delete1 (a, xs) = match xs with | [] -> [] | x::xs' -> if x = a then xs' else x::delete1(a, xs') let rec delete2 (a, xs) = match xs with | [] -> [] | x::xs' when x = a -> xs' | x::xs' -> x::delete2(a, xs') Another example is solving quadratic functions: type Solution = | NoRoot | OneRoot of float | TwoRoots of float *

F# Incomplete pattern matches on this expression when using “when”..Why?

和自甴很熟 提交于 2019-12-01 02:50:37
I have this simple F# function: let compareNum x = let y = 10 match x with | _ when x = y -> 0 | _ when x > y -> 1 | _ when x < y -> -1 However, F# compiler gives me "Incomplete pattern matches on this expression" warning. In this case, all cases should cover every pattern. I also see a similar example in "Pattern Matching" section in the 1st edition of Programming F# book by Chris Smith. So something might be changed in the later version of F#? Tomas Petricek I think the answer to the previous question (and the comments -- "In general, it is an anti-pattern to have a when guard in the last

F# Incomplete pattern matches on this expression when using “when”..Why?

断了今生、忘了曾经 提交于 2019-11-30 23:18:49
问题 I have this simple F# function: let compareNum x = let y = 10 match x with | _ when x = y -> 0 | _ when x > y -> 1 | _ when x < y -> -1 However, F# compiler gives me "Incomplete pattern matches on this expression" warning. In this case, all cases should cover every pattern. I also see a similar example in "Pattern Matching" section in the 1st edition of Programming F# book by Chris Smith. So something might be changed in the later version of F#? 回答1: I think the answer to the previous

Refactoring Guard Clauses

别来无恙 提交于 2019-11-29 22:18:19
What approaches do people take (if any) in managing guard clause explosion in your classes? For example: public void SomeMethod<T>(string var1, IEnumerable<T> items, int count) { if (string.IsNullOrEmpty(var1)) { throw new ArgumentNullException("var1"); } if (items == null) { throw new ArgumentNullException("items"); } if (count < 1) { throw new ArgumentOutOfRangeException("count"); } ... etc .... } In the project that I am currently working on there are many classes that have a similar set of guard clauses on the public methods. I am aware of the .NET 4.0 Code Contracts however this is not an