Questions tagged [guard-clause]

In function definitions by clauses with pattern matching, guards are Boolean expressions which augment pattern matching with the possibility to skip a pattern even if an argument structure matches the pattern.

In function definitions by clauses with pattern matching, guards are Boolean expressions which augment pattern matching with the possibility to skip a pattern even if an argument structure matches the pattern.

See also:

References

64 questions
7
votes
5 answers

Closest C# equivalent to the F# match expression?

I'm in the situation where a lot of my classes are containers of well-known but unordered objects of different types, e.g. a container may look as follows: public class Container { public A A { get; private set; } public B B { get; private…
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
6
votes
2 answers

vim + c++: insert a uuid in a guard clause

I'm trying to automate file comment headers. I'm stuck trying to figure out how to insert the result of the uuidgen command into my header using vim's autocmd. Inside the header, the placeholder text is present, like this: #ifndef _UUID_ #define…
J. Polfer
  • 12,251
  • 10
  • 54
  • 83
6
votes
3 answers

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

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…
sawa
  • 165,429
  • 45
  • 277
  • 381
5
votes
2 answers

Haskell pattern matching with guards

Suppose I want to model a tree structure in Haskell with data Tree = Null | Node Tree Integer Tree deriving Show and I'd like to test if every entry is, say, less than 10. I thought I would use pattern matching and write isSmall :: Tree ->…
Addem
  • 3,635
  • 3
  • 35
  • 58
5
votes
2 answers

Incomplete pattern match when two patterns share a `when` clause

A common surprise for beginning F# programmers is the fact that the following is an incomplete match: let x, y = 5, 10 match something with | _ when x < y -> "Less than" | _ when x = y -> "Equal" | _ when x > y -> "Greater than" But I just…
rmunn
  • 34,942
  • 10
  • 74
  • 105
5
votes
3 answers

return early vs if in ruby code

I see two styles of writing the same thing: def find_nest(animal) return unless animal.bird? GPS.find_nest(animal.do_crazy_stuff) end vs def find_nest(animal) if animal.bird? GPS.find_nest(animal.do_crazy_stuff) end end Which one is…
Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102
5
votes
4 answers

F# pattern match directly against let binding

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…
NickL
  • 1,870
  • 2
  • 15
  • 35
4
votes
3 answers

patterns vs. guards: otherwise does not match?

The following two functions behave differently when given an empty string: guardMatch l@(x:xs) | x == '-' = "negative " ++ xs | otherwise = l patternMatch ('-':xs) = "negative " ++ xs patternMatch l = l Here my…
4
votes
3 answers

How to write a guard clause with multiple conditions in Ruby?

After running Rubocop against this code I am getting Use a guard clause instead of wrapping the code inside a conditional expression. So from what I have read a "Guard Clause" will bail out of the method if the condition is not met, so we don't…
Richlewis
  • 15,070
  • 37
  • 122
  • 283
4
votes
2 answers

How to write a startsWith list function using an Active Pattern instead of when guard?

I need to check if a list starts with another, shorter list. The function, when using when guards is trivial: let rec startsWith l1 l2 = match l1, l2 with | [], _ | _, [] -> true | x::xs, y::ys when x = y -> startsWith xs ys | _ ->…
Sergey Aldoukhov
  • 22,316
  • 18
  • 72
  • 99
4
votes
1 answer

State Monad - While-loops

This question has been inspired by this question. I understand the example (ListBuilder) but I have not been able to create a while loop for my state monad. What is not clear to me is how to bind the body of the whileloop as the iterations follow…
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
3
votes
1 answer

Pattern match guard with DateTime.TryParseExact?

How to guard with DateTime.TryParseExact (and get the parsed value if possible)? The following code doesn't work. [] let main args = let argList = args |> List.ofSeq match argList with | "aaa" :: [] -> aaa.main "aaa" |…
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
3
votes
1 answer

fsunit.xunit test exception in constructor

Having type Category(name : string, categoryType : CategoryType) = do if (name.Length = 0) then invalidArg "name" "name is empty" i'm trying to test this exception using FsUnit + xUnit: [] let ``name…
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
3
votes
2 answers

Incomplete pattern matching a tuple in F#

I define a point type TimeSeriesPoint<'T> = { Time : DateTimeOffset Value : 'T } and a series type TimeSeries<'T> = TimeSeriesPoint<'T> list where I assume the points in this list are ordered by time. I am trying to zip two time series,…
Panos
  • 617
  • 7
  • 20
2
votes
4 answers

Guard Clause and Exception handling for the same condition

I ran across the following snippet of code. Names have been changed to protect the innocent: public void RunProgram() { System.IO.FileInfo fInfo = new System.IO.FileInfo(Application.StartupPath + "Program.exe"); if…
Kenneth Cochran
  • 11,954
  • 3
  • 52
  • 117