Let's say I have the following SQL statement:
SELECT 1 + myField + 2
If we want to reduce the expression at the parsing phase before sending it to SQL, we could turn it into:
SELECT 3 + myField
What is the name of the rule that allows this? Another example being:
SELECT myField * (1 + myField * 2 + 3)
Would turn into:
SELECT myField * (4 + (myField * 2) )
That's usually called "constant folding" (which should be a good search term) and it's easier to do as a transformation on the AST than trying to do it while you parse, precisely because of the reordering which is evident in your examples.
Related
What formula should I use in Google Sheets to get a sum of multiple cells, where every cell has an independent condition determining wether it should be included in the sum or not. Something like this:
result = 0 + (IF(condition1, A1)) + (IF(condition2, B1)) + (IF(condition3, C1))
Depending on your conditions, you can use something similar to this:
=SUM(IF(A1<>0,A1,0),IF(B1<10,B1,0),IF(C1<10,C1,0),D1)
Therefore, as a general example, you can simply make use of the SUM and the IF functions:
=SUM(IF(CONDITION1,A1,0),IF(CONDITION2,B1,0),IF(CONDITION3,C1,0)...)
the elements of the SUM function are separated by a comma;
the IF returns the first value if the condition is met and the second one if the condition is not met.
Reference
SUM function;
IF function.
Turns out it works the way I posted in Original Question:
result = 0 + (IF(condition1, A1)) + (IF(condition2, B1)) + (IF(condition3, C1))
BUT you need to use a different parameter separator if comma (,) is used as a decimal point separator in your language. For Polish I had to use semicolon (;)
result = 0 + (IF(condition1; A1)) + (IF(condition2; B1)) + (IF(condition3; C1))
In my experiment, users have the choice between 10 items per round they can select or leave the checkbox emtpy. In the next step, I'd like to create a new variable, e.g. MyInputR1, which holds the values of the previous checkboxes in the right order and as 1 new number.
My approach so far:
a)
Formatting input data: f1=format(D11,0.2).
Combining the input data and storing the information in a new variable: f = f1 + f2 + f3 + ....
Creating the variable MyInputR1 = stringtonumber(f)
b) Combining the input data (with values 0 or 1): MyInputR1 = D11 + D12 + D13 + D14 + ...
Unfortunately, the logic does not sum up and ztree does not understand what I am trying to do.
Thus my question:
Is it possible to combine / string together input data into 1 new variable, instead of adding it up?
Input data: checkbox with values 0 or 1
in total 10 input variables (D11 - D110)
Looking for a variable that e.g. looks like this: MyInputR1 = 0000011111
ztree code
Thanks for your help!
I'm trying to pattern match a binary against this
<<_:(A * ?N + A + B)/binary,T:1/binary,_/binary>>
However it seems erlang throws an error saying that variable T is unbound. Just a quick explanation: I want to ignore a certain number of bytes and then read a byte and then ignore the remaining bytes. How can I achieve this?
In bit syntax we can't use runtime expressions as bit size.
We can use only constants, compile time expressions like _:(4*8)/binary and variables: _:Var/binary.
In your case, solution is to bind A * ?N + A + B to variable first.
IgnoredBytes = A * ?N + A + B,
<<_:IgnoredBytes/binary,T:1/binary,_/binary>> = SomeBinary,
T.
It's Better explained in answer from [erlang-questions]
I got this as an interview question.
What is the maximum number of elements that can be on stack at a
specific moment while doing a translation from infix form to reversed
postfixed Polish form?
I know that the principle is that on the stack there cannot be elements with higher priority (usually * and /) under the ones with smaller priority (+ and -). I tried making an algorithm keeping track of a global and local maximum number, but I didn`t found out a certain rule.
For example if i have infix: 2 - 3 * 4 * 5 / 1 + 10
Stack 1: - * * / => maxLocal = 4 maxGlobal = 4
Stack 2: (After eliminating /, * and * because + has lower priority) - +
=> maxLocal = 2 maxGlobal = 4
Can you please help me?
I think there is no limit. For example, take the following infix expression: (1 + (1 + (1 + (1 + (1 + (1 + (1 + … It is very deep, every time you push yet another element to the stack. Of course there is usually some limit to the number of parentheses you parser accepts, but such limit is purely practical (to prevent stack overflow), not theoretical.
I'm writing an interpreter. I've done that before but never tried one which can work with expressions like 3 + 4 * 2 / ( 1 − 5 ) ^ 2 ^ 3.
I'm not having a problem with the parsing process, actually it is about my VM which then executes the code.
My goal was a fast interpreter and so I decided not to use a stack-based VM where you would need more than one instruction for a multiplication, for example (push, push, mul)
The "assembly" code for the VM generated by the parser looks as following:
3 + 4 * 2 / ( 1 − 5 ) ^ 2 ^ 3
becomes
sub 1 5
pow result 2
pow result 3
div 2 result
mul 4 result
add 3 result
(The result is correct)
As you can see: Every instruction takes no, one or two arguments. There is the result register which holds the result of the last instruction. And that's it.
Can a VM with a language of this structure and only one register calculate every mathematical expression for example Python or PHP can?
If it is not possible without a stack I'll start over right now!
What do you do about (1 + 2) * (3 + 4), or any other that would require you to calculate more than one intermediate result?