In Clojure, if I'm writing a test function and want to return boolean values from it, what's the right way to express these?
true and false are perfectly valid in Clojure
Give me some Clojure:
> true
true
> false
false
> (type true)
java.lang.Boolean
> (type false)
java.lang.Boolean
Related
In all other programming languages I've encountered, if statements always require a boolean to work, however in Lua the following code does not contain any errors. What is being checked in this if statement if both true and false statements can't be made? How can you only check "if (variable) then"? I am new to programming and currently working with Roblox Studio, any help would be very appreciated.
function onTouched(Obj)
local h = Obj.Parent:FindFirstChild("Humanoid")
if h then
h.Health = 0
end
end
script.Parent.Touched:Connect(onTouched)
Most languages have their own rules for how values are interpreted in if statements.
In Lua, false and nil are treated as false. All other values are treated as true.
if h == nil (null)
So if it couldn't find a humanoid in the object that touched the script's parent, it will be false (null), otherwise true (not null).
So
if [ObjectName] then
equals to if [ObjectName] != null then
*Only valid for objects (non primitive values)
It's like that in script languages.
if h then end
is basically equivalent to
if h ~= nil and h ~= false then end
In Lua all values that are not nil or false are considered to be logically true.
if h then end is usually used to check wether h is not nil. So if code depends on wether h has been defined you put it in a condition like that.
In your example h is being index. indexing nil values is not allowed. So befor you index it you should make sure it isn't nil to avoid errors.
Checking return values of functions is good practice.
I would like to filter a list by only keeping items that contain dimension or that contain metric and not penetration
I can filter to those that contain dimension OR metric and penetation, but I can't see how to switch the logic of the second case to metric and not penetration
Example below:
> library(stringr)
> var_list <- c("other", "dimension_1", "dimension_2", "metric_1", "metric_2", "metric_3_penetration")
> str_detect(var_list, "dimension|(?=.*metric)(?=.*penetration)")
[1] FALSE TRUE TRUE FALSE FALSE TRUE
Result that I would like to return from the str_detect is below:
[1] FALSE TRUE TRUE TRUE TRUE FALSE
You can use a combination of a negative and positive lookaheads for the second case:
> library(stringr)
> var_list <- c("other", "dimension_1", "dimension_2", "metric_1", "metric_2", "metric_3_penetration")
> str_detect(var_list, "dimension|^(?=.*metric)(?!.*penetration)")
[1] FALSE TRUE TRUE TRUE TRUE FALSE
The ^(?=.*metric)(?!.*penetration) regex matches when a string has metric and does not have penetration.
To only check for whole words, add (?:\b|_) boundaries:
str_detect(var_list, "dimension|^(?=.*(?:\\b|_)metric(?:\\b|_))(?!.*(?:\\b|_)penetration(?:\\b|_))")
A logical combination of grepl calls is simple and involves no packages:
grepl("dimension",var_list) | (grepl("metric",var_list) & !grepl("penetration",var_list))
## [1] FALSE TRUE TRUE TRUE TRUE FALSE
This might be a silly question. It's certainly pedantic.
Is there any difference between MyModel.where(...) and MyModel.where(...).all?
This question was prompted by a deprecation warning I got a few minutes ago:
DEPRECATION WARNING: This dynamic method is deprecated.
Please use e.g. Post.where(...).all instead.
Is that all really necessary?
No, they are equivalent, at least in the way you're probably thinking:
Model.where(id: nil) === Model.where(id: nil).all # returns true
Model.where(id: nil) == Model.where(id: nil).all # returns true
Model.where(id: nil).eql? Model.where(id: nil).all # returns false
Model.where(id: nil).equal? Model.where(id: nil).all # returns false
What these equalities tell us (borrowing heavily from this exceptional answer):
Case Equality- sometimes thought of as "If I have a box labeled a, does b get put in it?
"Equality" in the usual thinking. Are these two things the "same", can I replace one with the other without any impacts, do they return the same thing with the same input
.eql? and .equal? being false confirms that these are two different objects. Both that their hashes are non-identical and their object identities are non-identical
Can someone please explain these functions for me in racket. I`m totally lost. Please help me with some examples. Thanks! I just cannot figure these functions out for the life of me.
First, the If:
(if (positive? 1) 1 -1)
Racket first evaluates if 1 is positive (which is the first expresion (positive? 1)). If it is, returns 1, else, return -1. This is equivalent to c-like languages doing:
if ( positive?(1))
return 1
else
return -1
The Cond is basically a if that has multiple options. The equivalent in C-like languages would be else-if
(cond [(first-condition) (what-to-do)]
[(second-condition) (what-to-do)]
[(third-condition) (you-get-the-idea)])
And and Or are just the logical operators, equivalent to && and || in C-like languages
(and true true) => true
(and true false) => false
(or true true) => true
(or true false) => true
(or false false) => false
If is the turnary operator. It has three arguments, unless the first argument has a value of #f it will return the value of the second argument, otherwise the value of the third argument.
OR accepts one or more arguments, evaluates them one at a time left to right, and returns the first value if finds that is not #f, and returns #f if no argument satisfies that condition.
AND accepts one or more arguments evaluate them one at a time left to right, if it finds one that is #f it returns #f, otherwise it returns the value of the last argument that it evaluates.
COND accepts one or more arguments, each with one or more sub arguments (2 is the usual number though). It evaluates each argument left to right one at a time by evaluating the first subargument. If it is not #f then it evaluates each of the rest of the sub-arguments (if any) in order and returns the value of the last argument. Otherwise it moves onto the next argument and evaluates it the same way. else is a special sytax within these sub-arguments that is always treated as if it is not #f.
And here argument is understood to mean any valid s-expression.
NB: If you are familiar with non-lisp languages this is the answer for you. I'm not trying to explain them in other way than other code. Other answers do however so this is just a supplement.
None of those are functions, but special forms.
(if predicate
consequent
alternative)
is pretty much like Algol-dialects if
if( predicate )
{
consequent
}
else
{
alternative
}
Note that predicate, consequent and alternative can be anything from a complex expression to a simple value.
cond works more like an if, elseif..., else:
(cond (predicate1 consequent1)
(predicaten consequentn)
(else alternative))
and works like && in algol dialects. Thus false && print("Do it!") doesn't print anything since it short circuits.
(and #f (display "Do it!")) ; ==> #f (and does not display anything since first term was false)
(and 1 2 3) ; ==> 3 (3 is the last true value. In Scheme everything except #f is true.)
or works like || in algol dialects. thus true || print("Do it!") doesn't print since first term was true.
(or 'mama (display "Do it")) ; ==> mama (first true value, does not print "do it")
I'm currently trying to validate if a string is exactly true or false as I'm building a spreadsheet importer using CF10.
My users will either enter in one of the following.
NULL, which will equate to false
TRUE which obviously equates to true
FALSE which again will equate to false.
I'm trying to use the isValid('boolean',variable.data) to validate if the data is in fact a boolean string or not. However after reading quite a few posts I can see that it will validate true if its a positive number and false if it is a negative one etc.
I'm wondering if anyone has an easy fix to getting boolean validation working for strings easily ? Is this more a regular expressions scenario to make it work properly ?
Any help greatly appreciated.
Assuming you mean the literal string "NULL", one option is using list functions. Search a list of allowed values, ie "true,false,null". If the entry is found, it is valid:
<cfif listFindNoCase("true,false,null", theValue)>
this is a valid boolean value
<cfelse>
not found. do something here...
</cfif>
Then a simple string comparison would return false for everything other than the literal string "true":
isTrue = compareNoCase(e, "true") eq 0 ? true : false`
If you need to evaluate whether a string is exactly "true" or "false", then you are not doing a boolean comparison, you are doing a string comparison.
So to that end, you'd be wanting to use compare() or compareNoCase() (depending on how rigid you need to be).
You could just do some logic or am I missing something.
<cfset booleanResult = enteredValue eq 'TRUE' ? true : false />