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
1
vote
1 answer

How do I do early return in ruby to reduce nested if?

I always use early return to reduce nested if (programing with other languages) like this: //instead of nested if like this if (condition1) { if (condition2) { if (condition3) { //... finally meet all conditions //do the job…
Qiulang
  • 10,295
  • 11
  • 80
  • 129
1
vote
2 answers

F# a function as an argument in a match function

I have made function that takes a list and a list of lists and returns a new list of lists. let rec calculator list SS = match (List.item(0) SS) with |[] -> [] |_ -> match (validate list (List.item(0) SS)) with |(validate theCode list)…
Nulle
  • 1,301
  • 13
  • 28
1
vote
1 answer

Guard Clauses and the Single Responsibility Principle (SRP)

Currently reading the fantastic book 'Practical Object Orientated Design In Ruby' and working through a simple kata to really practice some of the principals it discusses. I have a method that does the following: def release_bike …
Joe
  • 678
  • 9
  • 24
1
vote
1 answer

Guard clause for properties that are possibly forgotten to be assigned a value

We have a message that is being passed to a method. class Message { public int TransactionId { get; set; } public bool IsCredit { get; set; } // Debit when false public decimal Amount { get; set; } } class…
Michael Buen
  • 38,643
  • 9
  • 94
  • 118
1
vote
3 answers

Ruby: Return if method returns a non-nil value

Is there a better/cleaner way to do this in Ruby? def my_method(x, y) return error if (error = validate(x, y)) # do something else end I call #validate elsewhere, so to keep things DRY, I have it return the error message.
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
1
vote
2 answers

Prolog add to list if element is not equal to string

I am trying to create a list of pairs which have a non-null Guard element. get_only_guarded(L) :- Guard \= null, findall([S,D],transition(S,D,_,Guard,_),L). This is what ive tried but it doesnt seem to work. What would be the correct way?
1
vote
2 answers

Simplifying nested pattern matching F#

I am writing a simple expressions parser in F# and for each operator I want only to support a certain number of operands (e.g. two for Modulo, three for If). Here is what I have: type Operator = | Modulo | Equals | If let…
0
votes
1 answer

constexpr guard clause does not compile

I wanted to add a constexpr guard clause in my code in order to avoid unnecessary indentation, but ran into this issue. This compiles: #include #include template void inc(T& t) { if constexpr…
Fredrik Enetorp
  • 414
  • 4
  • 15
0
votes
0 answers

How to shorten multiple guarded clauses with cleanups?

I'm having trouble with shortening multiple guarded clauses with cleanup causing hundreds of lines for condition checks and repetition of code. This is making me sick of scrolling. How do you deal with this kind of problem? For example, if…
0
votes
1 answer

way to always call a function without repeating code inside a function with guard clauses

I have a function which looks like this: function myFunc(){ if (!condition1){ //some extra code (diff. from condition2 if-statement) doSomething(); return; } if (!condition2){ //some extra code (diff. from condition1…
0
votes
1 answer

How to solve Haskell parse error on input 'otherwise'

I have a function which returns a list of halves of palindromes found from the input list. It works if I use an if-statement on one row but I'd like to use guards. Guards give me a parse error. I read many cases giving this kind of error, but I…
jvkloc
  • 623
  • 1
  • 5
  • 13
0
votes
1 answer

Guard clause for a single statement function?

What is the most readable way of writing a very simple function that is effectively executing one statement, if a condition is met? What I find most readable is this: function doSomething(myNumber){ if(myNumber !== null && myNumber > 5){ …
0
votes
2 answers

Using guard clause in ruby on rails for multiple independent if clause

How to use the guard clause in the following scenario? The msg is capturing info in 2 independent if-clauses. def edible?(food_object) edible_type = ['fruit','vegetable','nuts'] food_list =…
Vinit Kumar
  • 55
  • 10
0
votes
1 answer

Why "Parse error in pattern: x >= y" when defining a function by guarded equation?

I try to use guarded equation to define a function. Why does it not work in GHCi? Thanks. Prelude> :{ Prelude| maxThree :: Integer -> Integer -> Integer -> Integer Prelude| maxThree x y z Prelude| x >= y && x >= z = x Prelude| y >= z = y Prelude|…
Tim
  • 1
  • 141
  • 372
  • 590
0
votes
2 answers

Is a single return with boolean logic equivalent to guard clauses?

I'm working on a Boolean function in C/C++ that verifies multiple conditions (that are Boolean functions themselves) and only returns true if all of them are true. Time ago I started using Guard Clauses instead of nested ifs, even though it…
Nedo
  • 121
  • 1
  • 11