What's the name of this operator "+=" ? - programming-languages

What's the name of this operator "+=" ?

It, along with -=, *=, etc., are called the augmented assignment operators in Python, and "compound assignment" operators everywhere else.

The name is "plus equal" operator!

In c# it's called the addition assignment operator.

That is the Addition Assignment operator.

+= is the plus and Equal operator.
If you assign a+=3
That means u assign the expression are a=a+3.

Related

puppet in not in code

I have this code...
if $hostname in $var_slave {
file {
"/var/spool/cron/mysql":
ensure => present,
owner => $mysqlUser,
group => $mysqlGroup,
mode => "0600",
source => 'puppet:///modules/eikonappdbcron/mysql-slave',
}
}
I want a way of checking if a value is not in a file.
I tried "if $hostname not in $var_master" but this doesn't work. After doing some research I believe I need to use an "!". I can't get the syntax correct.
Puppet does not have a single, combined "not in" operator. Instead, it has the in operator, and it has a general-purpose boolean negation operator (!). You can use these together to write a compound expression that evaluates the condition you want.
If you already realized that, then perhaps you ran into a problem with operator precedence. The negation operator has the higher precedence than does in. Indeed, ! has the highest precedence of any Puppet operator, so if its operand is intended to be a binary expression then you must enclose the operand in parentheses.
Thus, the negation of the boolean expression
$hostname in $var_master
is
! ($hostname in $var_master)

Can I check strings equality in lua?

Just a straight forward beginner question, I am coding Lua stuff for Garrys Mod, learning by reading wiki and other codings.
if (self.Owner:SteamID( ) == "STEAM_0:1:44037488" ) then
the above is the code I want to use, to check to see if the STEAM ID (which I believe is a string) is equal to my exact string.
Is this viable? Or is there another way I should do it?
This should work exactly as you expect it to. In lua '==' for string will return true if contents of the strings are equal.
As it was pointed out in the comments, lua strings are interned, which means that any two strings that have the same value are actually the same string.
One thing to consider while learning Lua (from www.lua.org/source/5.2/lstring.h.html):
/*
** as all string are internalized, string equality becomes
** pointer equality
*/
#define eqstr(a,b) ((a) == (b))
String comparison in Lua is cheap, string creation may be not.
According to http://wiki.garrysmod.com/page/Player/SteamID, SteamID() returns a string so you should be able to write
if self.Owner:SteamID() == "STEAM_0:1:44037488" then
...do stuff...
end
If you ever need to confirm the type of an object, use type and print, like in this case print('type is', type(self.Owner:SteamID())) should print 'type is string'.
In lua, as answered above, '==' checks for equality.
Not saying you did this, because you didnt, but a common mistake is thinking that '=' is equality. '=' is assignment, '==' is equality.

What is the definition of double right arrow operator ( >> ) in Spock?

There are code snippets in this Spock wiki page that involve double right arrow operators (>>). For example:
setup:
subscriber.isAlive() >> true
So, what does the >> operator mean in this case? Thank you very much.
The right-shift (>>) operator defines the return value or behavior of a stubbed method. You can find more information under Stubbing in the reference documentation.

Does the shorthand ternary operator make multiple calculations?

The ternary operator has a shorthand version, i.e.
var = exp ?: exp2
I know it works in PHP. Other languages may have also picked it up. C# has similar functionality (for the context of this question) - ??.
When the condition passes, is the expression evaluated again, or is the result stored somewhere?
http://msdn.microsoft.com/en-us/library/ms173224.aspx
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.
It is stored, not computed twice.

Remembering the Ternary Operator Syntax

Anyone have a good trick to remember the standard ternary syntax?
Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years.
The condition you are checking is kind of like a question, so the question mark comes first.
x > 0 ? 1 : 0
Think of this statement as three English sentences: "Is x greater than 0? Then 1. Else, 0." You have one sentence for each clause in the statement.
The predicate:
x > 0 ? /* Is x greater than 0? */
The "true" branch:
1 /* Then 1. */
The "false" branch:
: 0 /* Else, 0. */
As far as remembering which symbol comes first, I just think of the fact that the first part is a question, "Is it true or not?", so the question mark goes first.
I think of the syntax in this manner
Question ? Yes : No
in python I read it as a normal English sentence:
a equals b if condition is true else c
Think of it this way: a ternary statement consists of three parts: the question, the code to execute if the answer to the question is "yes" and the code if the answer is "no". The "?" comes after the question like it does in English sentences.
"?" is a question mark so it means "if".
A colon means, "now it comes", "then do".
The good thing about the ternary operator is that you are not forced to use it, especially if you are having problems remembering the syntax. Just use an if-statement which is more readable most times.
And no - the ternary has no better performace then an if-statement.
It goes like this:
myVariable = this.testMethod() ? 'value for true case' : 'value for false case'
The syntax of ternary operators is <condition> ? <if-true> : <if-false>
? means if.
But if you put ? in front of an expression like if, the syntax becomes ? <condition> <if-true> : <if-false>.
<condition> <if-true> is hard to distinguish, so you have to add other symbols to split it, which defeats the purpose of the ternary operator to simplify the syntax.
So we have to put ? comes after it to solve this problem, so it is an inverted setting, which can be seen as (<condition> ?) <if-true> : <if-false>.
which reads: If <condition> then <if-true>, otherwise <if-false>.
If you're unit tests still pass when you get it wrong, then either it doesn't matter or your tests aren't covering enough of the paths through the code. If there's too long a gap between typing it and getting a pass/fail from the tests, that's another issue. Very few little syntax nits matter in the presence of good, fast tests.

Resources