SpEL - Test multiple conditions in findAll - spring

I am writing a SpEL condition.
It should be a basic IF, THEN, ELSE condition.
Currently it is this way:
cus_FinalCostBoolean == true ? FormDetails.findAll{cus_ForceRecommended == true}.min{cus_LastNegotiationRound} : FormDetails.findAll{cus_ForceRecommended == false}.min{cus_LastNegotiationRound}
where:
cus_FinalCostBoolean is a boolean variable
cus_ForceRecommended is a boolean variable
cus_LastNegotiationRound is a decimal number
Currently what this condition does is the following:
IF cus_FinalCostBoolean IS TRUE
THEN calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = TRUE
ELSE calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = FALSE
I'd like editing it the following way:
IF cus_FinalCostBoolean IS TRUE
THEN calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = TRUE and cus_TechnicalEvaluationPassed = 'OK'
ELSE calculate min(cus_LastNegotiationRound) among all the entries that have cus_ForceRecommended = FALSE and cus_LastNegotiationRound is not null
where cus_TechnicalEvaluationPassed is a text variable
I was trying to editing it this way:
cus_FinalCostBoolean == true ? FormDetails.findAll{cus_ForceRecommended == true && cus_TechnicalEvaluationPassed == 'OK'}.min{cus_LastNegotiationRound} : FormDetails.findAll{cus_ForceRecommended == false && cus_LastNegotiationRound != null}.min{cus_LastNegotiationRound}
But it seems it does not work.
Could you help me?
Thank you.
Best regards.

Related

Kotlin elvis operator vs "... == true" boolean expression check preformance

Assume we have nullable val a: Boolean?.
In code we want to assign value of a to another non-nullable variable val b: Boolean.
If a is true then we want b also to be true
If a is false or null the we want b to be false.
We can do this in 2 ways:
b = a ?: false
or
b = a == true
Which of the following approaches is better in terms of performance?

How to alter Boolean value from true to false and vice-versa

I have a requirement in Go of altering the bool value and store it.
I am receiving value == true but I need to alter it and store the altered value.
I can think of only storing the alerted to a var and pass it in the next statement.
Eg psuedo code:
chnagevalue := false
if value == false {
changevalue == true
}
what's the best way to do it in Go? Is there any pre-defined way to do it?
Use the logical NOT operator ! to change true to false and vice versa:
changedValue := !value
There's a short answer, written somewhere else :-)
Yours is almost good too:
changedValue := false
if !value {
changedValue == true
}
An if statement is always about something being true.
so in the above it reads : if a value equals false is true then {}.
of course your mind reads it the short way :
if a value equals false then {}.
the switch of course works the best:
changedValue := !changedValue
BTW: I would never use a fieldname like "changedValue" because...
every variable is a value of some kind, so there is no need of writing that.
"changed" should be enough.
or, when it is a boolean like this , even better:
"isChanged" or "hasChanged"

How do the two R codes, which should do the same thing (at least in my head), differ?

In order to replicate this code, you will need these packages:
Tidyverse, Nycflights13
I am basically trying to understand why two pieces of code, which in my head should do the same thing, don't do the same thing.
I am currently learning R for data science from R for Data Science by Garrett Grolemund & Hadley Wickham, and I've gotten to a point which the code begins to confuse me quite a bit, which hopefully is normal! I will write down the two pieces of code which confuse me in why they don't do the same thing!
filter(flights, dest == c("HOU","IAH"))
#and
filter(flights, dest == "HOU" | dest == "IAH")
I expected both of these codes to show the same amount of rows but the first one shows 4658 rows (the wrong amount), where as the second one shows 9313 (the right amount).
What I wanted to do is to shorten the code by using (filter(flights, dest == c("HOU","IAH"))) instead of (filter(flights, dest == "HOU" | dest == "IAH"))
but it yields different results, which gravely confuses me!
Please give me your advice, I am a newbie!
Because filter(flights, dest == c("HOU","IAH")) is the same as
filter(flights, dest == c("HOU","IAH"))
flights$dest = c("HOU","IAH")
* # For demonstation purposes, I'm assuming the flights dataset has has 4 rows, as:
c("HOU","CPH","IAH","EDI") == c("HOU","IAH")
Now, a vector of 4 elements cannot possible be the same as a vector of 2 elements. And this performs an element-wise comparison, "HOU" == "HOU", "CPH" == "IAH", "IAH" == ???. So R "helps" us by repeating the shorter vector. The output is thus:
> c("HOU","CPH","IAH","EDI") == c("HOU","IAH")
[1] TRUE FALSE FALSE FALSE
Try extending with a 5th element:
> c("HOU","CPH","IAH","EDI", "LDN") == c("HOU","IAH")
[1] TRUE FALSE FALSE FALSE FALSE
Warning message:
In c("HOU", "CPH", "IAH", "EDI", "LDN") == c("HOU", "IAH") :
longer object length is not a multiple of shorter object length
and you broke it.
So the 1st line only makes sense if and only if flights has 2 rows, whereupon an element-wise comparison can be performed.
However, what you might be looking for is the %in% operator:
> c("HOU","CPH","IAH","EDI") %in% c("HOU","IAH")
[1] TRUE FALSE TRUE FALSE
> c("HOU","CPH","IAH","EDI","LDN") %in% c("HOU","IAH")
[1] TRUE FALSE TRUE FALSE FALSE
which can be expanded to filter(flights, dest %in% c("HOU","IAH")) and luckily works for vectors of any length.

Test spec failing with my prime number checker

I am doing some coding challenges at code wars and ran into this one asking me to make a method that takes a number and determines if it is a prime number or not. If it is prime the method should return "true" if the number is not prime it should return "false".
The method passes every introductory test and each number I can think to throw at it but continually kicks back two tests as incorrect. At this point I'm curious if I am not understanding something about the testing process?
This is my code:
def isPrime(num)
counter=2 #is incremented with every time until loop loops
until counter>999999999999999 do
if num.abs <2
return false
elsif num.abs % counter == 0 && num.abs!=counter
return false
else
return true
end#if
counter+=1
end#
end```
and this is the feed back that code wars is sending back to me
isPrime
Should have isPrime defined.
Test Passed
Should return false for numbers less than 2.
Test Passed: Value == false
Test Passed: Value == false
Test Passed: Value == false
Should return false for non-prime numbers.
Test Passed: Value == false
Test Passed: Value == false
Expected: false, instead got: true # THESE ARE THE TESTS THAT FAIL
Expected: false, instead got: true # THESE ARE THE TESTS THAT FAIL
Should return true for prime numbers.
Test Passed: Value == true
Test Passed: Value == true
Test Passed: Value == true
Test Passed: Value == true
Test Passed: Value == true
Also I checked here page for help on the algorithm.
Any help is greatly appreciated.
There are a number of issues here. The biggest is with the if statement you have inside the loop.
if num.abs <2
return false
elsif num.abs % counter == 0 && num.abs!=counter
return false
else
return true
end
There is no condition under which this if statement will not terminate the loop and return either true or false in the first iteration. This will prevent counter from ever being incremented.
The next issue is in your loop control. You have
until counter>999999999999999 do
...
counter+=1
end
In this case it would be better to stop at the sqrt(num) rather than some large number. For better performance you probably should instead use as the loop control something like
until counter*counter > num do
This will avoid multiple sqrt calculations. You could precompute the sqrt instead, something like
sqrt_num = num.sqrt
until counter > sqrt_num do
(I don't know Ruby so I may have the syntax wrong, but I think you get the point). If you do this, though, be sure num is not negative before hand.
If you exit from the loop never having found a factor for num you know the number is prime.
There are 2 problems in your code
Why are you taking the num.abs? If the tester provides negative numbers, it doesn't fail. By formal definition negative numbers aren't prime.
The program can stop at Math.sqrt(n) https://stackoverflow.com/a/5811176/3804420

Ruby - how to assign multiple variables based on another boolean variable?

I am trying to do:
the_tag= line[2..5]
rec_id_line = (line[2]=='#')? true : false
new_contents,new_to_close=
rec_id_line? open_id_tag(indent,line) : open_tag(indent,the_tag,last_field)
Those two methods both return two values (btw I'm refactoring here)
i.e. for the two variables, I either want to call open_id_tag(2 params) otherwise open_tag(3 params) depending on the true/false rec_id_line value.
You just have to put a space between rec_id_line and ?:
new_contents, new_to_close = rec_id_line ? open_id_tag(indent, line) : open_tag(indent, the_tag, last_field)
Furthermore line[2]=='#' probably returns a boolean value so can simplify your second line:
rec_id_line = (line[2] == '#')
Or combine both lines:
new_contents, new_to_close = (line[2] == '#') ? open_id_tag(indent, line) : open_tag(indent, the_tag, last_field)

Resources