I have the following two functions:
find(list, closure) := block(
filter: sublist(list, closure),
if length(filter) > 0 then filter[1] else null)$
find_index(list, closure) := block(
filter: sublist_indices(list, closure),
if length(filter) > 0 then filter[1] else null)$
In other languages I would return null or nil here, if the element that is searched can't be found. But I think this keyword doesn't exist in maxima. What is the common convention in maxima for what to return in this case? Maybe false?
false is indeed the most commonly used value to represent "not there". It is not required, but there is a pretty consistent convention for it.
Note that if foo then bar evaluates to false if foo evaluates to false. Therefore if foo then bar else false is equivalent to if foo then bar, which is a little shorter.
From the manual:
-- Constant: false
'false' represents the Boolean constant of the same name. Maxima
implements 'false' by the value 'NIL' in Lisp.
So the lisp equivalent of an empty result is false.
Related
There seems to be a difference between these two checks:
if not object then
if type(object) == "nil" then
However I really don't understand the difference.
if type is "nil", shouldn't a not object then also work?
Lua, like many other dynamically typed languages, has the concept of "truthy" and "falsy", where boolean expressions can handle more values than just actual booleans.
Each non-boolean value has a specific meaning attached when used in a boolean expression. Specifically:
nil and false are "falsy"
everything else is "truthy"
That is why (not nil) == (not false), but type(nil) ~= type(false), because not x is a boolean expression that coerces x to truthy/falsy, while type() checks the actual type.
I'm just learning lua, and see these two ways to check for nil
local stats = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
if (stats ~= nil) then
-- do stuff
end
if (stats) then
-- do stuff
end
Are the if statements equivalent? If so, is there any advantage to including the extra "~= nil" part?
Statement "~= nil" works also if stats = false.
You can read in docs:
The condition expression of a control structure can return any value.
Both false and nil are considered false. All values different from nil
and false are considered true (in particular, the number 0 and the
empty string are also true).
What does this block of code do?
local name = value or 0
Please tell me that it makes it zero if nil and makes it value if not nil.
Short answer
yes
Looooong answer
You're right. While in other languages, the logical operators return either true or false, Lua (and some other languages) does something more clever:
When the first parameter of or is truthy, it evaluates to that value, if it's not, it evaluates to the second one. And does it the other way around: if its left-hand-side is falsey, it evaluates to that, otherwise it evaluates to its RHS.
Logically, this means that or evaluates to truthy if either operand is truthy and and evaluates to falsey if either of its operands is.
This is often used as an equivalent of
if value then
name = value
else
name = 0
end
And it effectively does the same. It is also often use to assign default values to variables like this:
function call(name)
name = name or "you"
print("Hey "..name.."! Come here for a moment!")
end
Note though, that this doesn't work
function alarm(real)
real = real or true
print "ALAAARM!"
if real then print "This is NOT a drill!" end
end
alarm(false)
This will always print "ALAAARM!" "This is NOT a drill!", because false is evaluated as falsey, so the or statement evaluates to its RHS, which is true. In this particular example, you would have to check explicitly if the argument is nil.
-- ...
real = (real == nil) and true or real
-- ...
This would work as intended, because only if real == nil, the and statement evaluates to true, and the or thus evaluates to its LHS. If real == nil is false, then the and evaluates to that, thus the or statement evaluates to its RHS (because its LHS is false).
It's also worth mentioning that both and and or are short-circuited. What this means is:
function foo(bar)
print(bar)
return bar
end
io.write "First: "
local A = foo(nil) or foo(false)
io.write "Second: "
local B = foo(true) or foo(true)
This will print "First: nil false" on the first two lines, but then "Second: true" on the third line. The last call to foo is not even executed, because at that point the or statement already knows it's going to return its left operand.
I have used repl.it to see what it returned, just for the sake of curiosity, and it turned out that
not nil
returnes true
Why is it? Is it because in Lua everything should be rather true or false in the end?
Repl.it link: https://repl.it/repls/SanePastelHarrier
Because nil is false when converted to boolean:
2.2 Booleans
The boolean type has two values, false and true, which represent the traditional boolean values. However, booleans do not
hold a monopoly of condition values: in Lua, any value may represent a
condition. Conditionals (such as the ones in control structures)
consider both false and nil as false and anything else as true.
Beware that, unlike some other scripting languages, Lua considers both zero
and the empty string as true in conditional tests.
And not treats its argument as a boolean:
3.3 Logical Operators
The logical operators areand, or, and not. Like control structures, all logical operators consider both false and nil
as false, and anything else as true.
I have a variable that's returned from the db as a string. I convert it to a number and then test like so:
if tonumber(v.active) == 1 then
elements.active.value = true
else
elements.active.value = false
end
The value in elements.active.value is being used to diplay a checkbox. I'm wondering if there's a way to combine this all into one statement?
EDIT 1
I'm using a lua boolean value to set the checkbox, so I can't use 1. You have to use true / false.
I'm not so much interested in whether or not I can use 1 or true to set the value. I'm more interested in keeping the logic the same, but simplifying.
What I was really after was something like what you can do in php like so:
max = array_key_exists ('max', $options) ? $options['max'] : 0;
it'll use either the $options['max'] value or 0 depending on the eval.
Like this:
elements.active.value = tonumber(v.active) == 1
Because the result of a relational operator like == is boolean, just what you assigned to elements.active.value in your piece of code.
Lua reference manual: Relational Operators
The relational operators in Lua are
== ~= < > <= >=
These operators always result in false or true.