Python: defining my own operators?

While technically you cannot define new operators in Python, this clever hack works around this limitation. It allows you to define infix operators like this: # simple multiplication x=Infix(lambda x,y: x*y) print 2 |x| 4 # => 8 # class checking isa=Infix(lambda x,y: x.__class__==y.__class__) print [1,2,3] |isa| [] print [1,2,3] <<isa>> [] # => True

What does

It can have 3 distinct meanings: ‘<<‘ as an ordinary method In most cases ‘<<‘ is a method defined like the rest of them, in your case it means “add to the end of this array” (see also here). That’s in your particular case, but there are also a lot of other occasions where you’ll … Read more

How does !!~ (not not tilde/bang bang tilde) alter the result of a ‘contains/included’ Array method call?

There’s a specfic reason you’ll sometimes see ~ applied in front of $.inArray. Basically, ~$.inArray(“foo”, bar) is a shorter way to do $.inArray(“foo”, bar) !== -1 $.inArray returns the index of the item in the array if the first argument is found, and it returns -1 if its not found. This means that if you’re … Read more

Conditional XOR?

Conditional xor should work like this: true xor false = true true xor true = false false xor true = true false xor false = false But this is how the != operator actually works with bool types: (true != false) // true (true != true) // false (false != true) // true (false != … Read more

Is there an “opposite” to the null coalescing operator? (…in any language?)

There’s the null-safe dereferencing operator (?.) in Groovy… I think that’s what you’re after. (It’s also called the safe navigation operator.) For example: homePostcode = person?.homeAddress?.postcode This will give null if person, person.homeAddress or person.homeAddress.postcode is null. (This is now available in C# 6.0 but not in earlier versions)

What does the question mark character (‘?’) mean in C++?

This is commonly referred to as the conditional operator, and when used like this: condition ? result_if_true : result_if_false … if the condition evaluates to true, the expression evaluates to result_if_true, otherwise it evaluates to result_if_false. It is syntactic sugar, and in this case, it can be replaced with int qempty() { if(f == r) … Read more