How do do formulate in Jekyll/Liquid language the following expression:
if (A and B) or (not C)
What are the rules globally (precedence, left,right) ?
Parentheses won't work, all precedence rules are detailed here:
https://shopify.dev/docs/themes/liquid/reference/tags/control-flow-tags#multiple-conditions-and-or
Related
Is there any substantial difference between operators and methods?
The only difference I see is the way the are called, do they have other differences?
For example in Python concatenation, slicing, indexing are defined as operators, while (referring to strings) upper(), replace(), strip() and so on are methods.
If I understand question currectly...
In nutshell, everything is a method of object. You can find "expression operators" methods in python magic class methods, in the operators.
So, why python has "sexy" things like [x:y], [x], +, -? Because it is common things to most developers, even to unfamiliar with development people, so math functions like +, - will catch human eye and he will know what happens. Similar with indexing - it is common syntax in many languages.
But there is no special ways to express upper, replace, strip methods, so there is no "expression operators" for it.
So, what is different between "expression operators" and methods, I'd say just the way it looks.
Your question is rather broad. For your examples, concatenation, slicing, and indexing are defined on strings and lists using special syntax (e.g., []). But other types may do things differently.
In fact, the behavior of most (I think all) of the operators is constrolled by magic methods, so really when you write something like x + y a method is called under the hood.
From a practical perspective, one of the main differences is that the set of available syntactic operators is fixed and new ones cannot be added by your Python code. You can't write your own code to define a new operator called $ and then have x $ y work. On the other hand, you can define as many methods as you want. This means that you should choose carefully what behavior (if any) you assign to operators; since there are only a limited number of operators, you want to be sure that you don't "waste" them on uncommon operations.
Is there any substantial difference between operators and
methods?
Practically speaking, there is no difference because each operator is mapped to a specific Python special method. Moreover, whenever Python encounters the use of an operator, it calls its associated special method implicitly. For example:
1 + 2
implicitly calls int.__add__, which makes the above expression equivalent1 to:
(1).__add__(2)
Below is a demonstration:
>>> class Foo:
... def __add__(self, other):
... print("Foo.__add__ was called")
... return other + 10
...
>>> f = Foo()
>>> f + 1
Foo.__add__ was called
11
>>> f.__add__(1)
Foo.__add__ was called
11
>>>
Of course, actually using (1).__add__(2) in place of 1 + 2 would be inefficient (and ugly!) because it involves an unnecessary name lookup with the . operator.
That said, I do not see a problem with generally regarding the operator symbols (+, -, *, etc.) as simply shorthands for their associated method names (__add__, __sub__, __mul__, etc.). After all, they each end up doing the same thing by calling the same method.
1Well, roughly equivalent. As documented here, there is a set of special methods prefixed with the letter r that handle reflected operands. For example, the following expression:
A + B
may actually be equivalent to:
B.__radd__(A)
if A does not implement __add__ but B implements __radd__.
My question is Python specific (3.4.3).
My question is specific to Built-In functions only.
It is clear to me the difference between a keyword (reserved word) and an identifier (user-defined variable).
See: https://en.wikipedia.org/wiki/Reserved_word
Likewise, I understand the basic meaning of the terminology 'function'
See : https://en.wikipedia.org/wiki/Functional_programming \
and
http://www.learnpython.org/en/Functions
However, I am having difficulty understanding the difference between Built-in functions and keywords; such as 'if' and 'for'.
https://docs.python.org/3/library/functions.html#built-in-funcs
What is the difference between the two? Keyword and Function.
Is the Keyword 'if' not simply a built in function? If so, why does it not appear in the official list of Built-In functions in the Python documentation?
https://docs.python.org/3/library/functions.html#built-in-funcs
It certainly behaves as a function. Is it simply because it preforms a procedure as opposed to returning a value? In which case how would you define it? As a method?
I have searched high and low on stackoverflow and I cannot seem to locate an answer.
Answers such as the two examples given below do not answer the overriding questions for me. Which are;
1) What defines a keyword as a keyword, rather than a builtIn function?
2) If keywords such as 'if' are not functions, then what are they? They are not classes etc. I understand that 'IF' is an example of a condition statement but what is the generic terminology for these keywords. The word keyword only defines the fact that it is reserved within the language, it does not define what the actual object is, i.e. function, class, method etc.
http://stackoverflow.com/questions/6054672/whats-the-difference-between-a-keyword-or-a-statement-and-a-function-call
http://stackoverflow.com/questions/155609/difference-between-a-method-and-a-function?rq=1
Keywords are those that describe the action to be performed, or specify how to interpret something (give meaning to instructions)
Functions are simply labels (for a set of instructions).
If you change function names it won't matter to Python (you can edit built in modules), but you can't relabel keywords.
You have already added tons of references to both, so I will not cite more.
The simplest way to solve precedence of operator?
for example
1+2*3/4%5
I need simplest and logical way to solve it?
"i dont want to use parentheses"
You may need to know operator precedence first, and thier associativity as well.
Here, *, / and % have same precedence but have higher precedence than +.
Since they all are lef-to-right associative, grouping them results in
1+(((2*3)/4)%5)
If they were right-to-left associative, it would have been
1+(2*(3/(4%5)))
If you don't want to use parenthesis, just make sure you write them in order like:
1+2*3/4%5
-> 1+6/4%5
-> 1+1%5
-> 1+1
-> 2
I hope you understand.
My question is the one stated in the title:
"Is the same to use || instead OR and && instead of AND in MYSQL?"
I know that normally you use "AND" or "OR" as comparison operators in SQL but it (seems that) work also "&&" and "||" (like in Java/Javascript etc.) in MYSQL. Is that correct?
Thank you for the aclaration
As others have said, they are indeed equivalent—with the following exceptions as documented under Operator Precedence:
The meaning of some operators depends on the SQL mode:
By default, || is a logical OR operator. With PIPES_AS_CONCAT enabled, || is string concatenation, with a precedence between ^ and the unary operators.
By default, ! has a higher precedence than NOT. With HIGH_NOT_PRECEDENCE enabled, ! and NOT have the same precedence.
See Section 5.1.7, “Server SQL Modes”.
Yes: http://dev.mysql.com/doc/refman/5.0/en/logical-operators.html
The manual is quite thorough :)
Have a look at http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html it is the same!
According to the documentation, yes, it is the same.
Same question goes for | and ||.
What are uses for the & and && operator? The only use i can think of are
Bitwise Ands for int base types (but not float/decimals) using &
logical short circuit for bools/functions that return bool. Using the && operator usually.
I cant think of any other cases i have used it.
Does anyone know other uses?
-edit- To clarify, i am asking about any language. I seen DateTime use '-' to return a timespan, strings use '+' to create new strings, etc. I dont remember any custom datatype using && and &. So i am asking what might they (reasonably) be use for? I dont know of an example.
In most C-based languages the meanings of these operators are:
&& - boolean AND. Used in boolean expressions such as if statements.
|| - boolean OR. Used in boolean expressions such as if statements.
& - bitwise AND. Used to AND the bits of both operands.
| - bitwise OR. Used to OR the bits of both operands.
However, these are not guaranteed to be such. Since every language defines its own operators, these string can be defined as anything in a different language.
From your edit, you seem to be using C#. The above description is right for C#, with | and & also being conditional operators (depending on context).
As for what you are saying about DateTime and the + operator - this is not related to the other operators you mentioned and their meaning.
If you're asking about all languages then I don't think it's reasonable to talk about "the & operator". The token & could have all sorts of meanings in different languages, operator and otherwise.
For example in C alone there are two distinct & operators (unary address-of and binary bitwise-and). Unary & in C and related languages is the only example I can immediately think of, of a use I've encountered that meets your criteria.
However, C++ adds operator overloading so that they can mean anything you like for user-defined classes, and in addition the & character has meaning in type declarations. In C++0x the && token has meaning in type declarations too.
A language along the lines of APL or J could "reasonably" use an & operator to mean pretty much anything, since there is no expectation that code in those languages bears any resemblance at all to C-like languages. Not sure if either of those two does in fact use either & or &&.
What meanings it's "reasonable" for a binary & operator overload to have in C++ is a matter of taste - normally it would be something that's analogous to bitwise & in some way, because the values represented by your class can be considered as a sequence of bits in some way. Doesn't have to be, though, as long as it's something that makes sense in the domain. Normally it's fairly "unreasonable" to use an & overload just because & happens to be unused. But if your class represents something fairly abstruse in mathematics and you need a third binary operator after + and *, I suppose you'd start looking around. If what you want is something with even lower precedence than +, binary & is a candidate. I can't for the moment think of any structures in abstract algebra that want such a thing, but that doesn't mean there aren't any.
Overloading operator&& in C++ is moderately antisocial, since the un-overloaded version of the operator short-circuits and overloaded versions don't. C++ programmers are used to writing expressions like if (p && *p != 0), so by overloading operator&& you're in effect messing with a control structure.
Overloading unary operator& in C++ is extremely antisocial. It stops people taking pointers to your objects. IIRC there are some awkward cases where common implementations of standard templates require of their template parameters that unary operator& results in a pointer (or at least a very pointer-like thing). This is not documented in the requirements for the argument, but is either almost or completely unavoidable when the library-writer comes to implement the template. So the overload would place restrictions on the use of the class that can't be deduced from the standard, and there'd better be a very good reason for that.
[Edit: what I didn't know when I wrote this, but do know now, is that template-writers could work around the need to use unary operator& with template parameters where the standard doesn't specify what & does for that type (i.e. all of them). You can do what boost::addressof does, which is:
reinterpret_cast<Foo*>(&reinterpret_cast<char&>(foo))
The standard doesn't require much of reinterpet_cast, but since we're talking about standard templates they know exactly what it does in the implementation, and anyway it's legal to reinterpret an object as chars. I think this is guaranteed to work - but if not the implementation can ensure that it does work if necessary to write fully conforming standard templates.
But, if your implementation doesn't go to these lengths to avoid calling an overloaded operator&, the original problem remains.]
As your previoes question about these operators has been about C#, I assume that this one is too.
Generally you want to use the short-circuit version of the conditional operators to avoid unneccesary operations. If the value of the first operand is enough to determine the result, the second operand needn't be evaluated.
When a condition relies on the previos condition being true, only the short-circuit operators work, for example doing a null check and property comparison:
if (myObj != null && myObj.State == "active")
Using the & operator in that case would not keep the second operand from being evaluated, and it would cause a null reference exception.
The non-shortcircuit operators are useful when you want both operands to always be evaluated, for example when they have a side effect:
if (DoSomeWork() & DoOtherWork())
Using the && operator would prevent the second method to be called if the first returned false.
The & and | are also binary operators, but as the || and && operators aren't, there is no ambiguity when you use them as binary operators.
Very general question and I'm assuming you're talking in Java, C#, or another similar syntax. In VB it's the equivalent of + on strings, but that's another story I assume.
As far as I know, your statement is correct if you're talking in terms of C#.
If it's Javascript then please look at this answer: Using &&'s short-circuiting as an if statement?
There is a short discussion on C# uses there too.
Java has a few more operators, such as |= : What does "|=" mean in Java?
C uses & as a unary operator on any data types to get the address of the data
for example:
int i = 5;
cout<<&i;//print the address of i
Some languages allow you to override such operators to make them do anything you want!