Incorrect expression evaluation with expr object in Pure Data - expression

I'm trying to print string message once the expression is true, right now the output still prints a message though the expression is false.

Actually there is no problem with your expression, but the subsequent dataflow. The bang object [bng] converts any input to a bang message. Even though the expression is correct, the output 0 and 1 will both be converted into a bang. the bang is then printed into the console as "text":bang.
Replace the bang GUI with a [select 1] object and you are almost there.
This is a more Pd-like syntax:
Also: Please read Is there any reason to use vanilla Pure Data instead of Pd-extended?

Related

Evaluating a frozen string

My vague understanding is that, with Ruby 2.2's frozen method on string or Ruby 2.3's frozen-string-literal: true pragma, a relevant frozen string literal is evaluated only once throughout program execution if and only if the string does not have interpolation. The following seems to illustrate this:
Not interpolated
#frozen-string-literal: true
5.times{p "".object_id}
Outputs (same object IDs):
70108065381260
70108065381260
70108065381260
70108065381260
70108065381260
Interpolated
#frozen-string-literal: true
5.times{p "#{}".object_id}
Outputs (different object IDs):
70108066220720
70108066220600
70108066220420
70108066220300
70108066220180
What is this property (i.e., being evaluated only once) called? It should be distinct from immutability.
Is my understanding of the condition when strings come to have such property correct? Where is the official documentation mentioning this?
Is there a way to make an interpolated string be evaluated only once?
Interning. The strings are said to be interned.
Not completely. It is more like if the interpreter can decide what the value of the string would be before evaluating it. For example, consider:
5.times { puts "#{'foo'}".object_id }
The id is the same even though there is interpolation involved.
No. This is an internal optimization. The main point of Object#freeze is immutability.
UPDATE: Only literal strings get internalized. This is evident here.
I couldn't find the part of the code responsible for interpolation. So I'm not sure why "#{'foo'}" is considered a literal string. Note that wherever this translation occurs, it is on a lower parser level and happens way before any actual processing. This is evident by the fact that String#freeze is mapped to rb_str_freeze, which doesn't call opt_str_freeze.
"Frozen" is not about whether the string is evaluated more than once. It is, you are right, about mutability.
A string literal will be evaluated every time the line containing it is encountered.
The (only) way to make it be evaluated only once, is to put it in a line of source code that is only executed once, instead of in a loop. A string literal in a loop (or any other part of source code) will always be evaluated every time that line of source code is executed in program flow.
This is indeed a separate thing than whether it is frozen/immutable or not, once evaluated.
The accepted answer is kind of misleading. "It is more like if the interpreter can decide what the value of the string would be before evaluating it." Nope. Not at all. It needs to be evaluated. If the string is frozen, then once it IS evaluated, it will use the same location in memory and the same object/object_id (which are two ways of saying the same thing) as all other equivalent strings. But it's still being evaluated, with or without interpolation.
(Without interpolation, 'evaluation' of a string literal is very very quick. With simple interpolation it's usually pretty quick too. You can of course use interpolation to call out to an expensive method though, hypothetically).
Without interpolation, I wouldn't worry about it at all. With interpolation, if you think your interpolation is expensive enough you don't want to do it in a loop -- the only way to avoid it is not to do it in a loop, but create the string once outside the loop.
Ruby docs probably talk about "String literals" rather than "literal Strings". A "String literal" is any String created by bytes in source code (using '', "", %Q[], or any of the other ways of creating strings literals in source code in ruby). With or without interpolation.
So what kinds of Strings aren't created by String literals? Well, a string created by reading in bytes from a file or network for instance. Or a String created by taking an existing string and calling a method on it that returns a copy, like some_string.dup. "String literal" means a string created literally in source code, rather than by reading from external input. http://ruby-doc.org/core-2.1.1/doc/syntax/literals_rdoc.html

What does "(...) interpreted as grouped expression" mean?

I'm using a Ruby linter in Atom and for some lines it gives the following warning:
(...) interpreted as grouped expression
An example of a line that get's this warning is this:
elsif not (params[:vacancy].nil? or params[:vacancy]['company_id'].nil? or params[:vacancy]['company_id'] == "0" )
How should that line be improved to make the warning go away?
The warning is
(...) interpreted as grouped expression
And it means exactly what it says: in Ruby, parentheses can be used for three purposes, expression grouping, parameter lists and argument lists. This warning is emitted when Ruby thinks that you want an argument list but wrote a grouped expression instead. The most common cause is whitespace between the name of the message and the argument list in a message send like this:
foo.bar (1, 2)
This will be interpreted not as an argument list for the message send, but rather a grouped expression, which, in this particular case, is a SyntaxError.
In your particular case, the warning seems to be a false positive.
Try to Remove the space between not and the parenthesis
The warning I get is from from MRI Ruby itself (with options -wc), and I think you have a typo in there. The message I get doesn't have the word "grounded" but "grouped".
Parenthesis in Ruby can be used for one of two things, to group expressions or to mark the argument list of a function or method.
What that error message is saying is that of these two options, Ruby is treating it like an expression. Note that it in Ruby is possible for you to define a method called "not".
And In this particular case it doesn't matter which way Ruby interprets the parenthesis.
One way to get rid of the message is to remove the space between "not (". If you think this is screwy, I agree with you.

How does this Ruby code work - if-stmt with ranges ?

I'm currently learning Ruby and I can't seem to wrap around what if /start/../end does... Help?
while gets
print if /start/../end/
end
Since you mentioned that you're new to Ruby, it's first worth taking note that you're dealing with Regular Expressions (regex) in the example - anything that is delimited between two forward slashes:
/start/ # a regular expression literal
Regular Expressions are a powerful way of matching a certain combination of letters from a larger string.
"To start means to begin." =~ /start/ #=> true, because 'start' is in the string.
The double dot notation is the flip-flop operator, a controversial construct probably inherited from Perl and not usually recommended to be used because it can lead to confusion.
It means the following:
It will collectively evaluate to false until the left hand operand is true. At which point it will collectively evaluate to true. However it will only remain true until the right hand operand evaluates to true - at which point it will again evaluate collectively to false.
Using your above example therefore:
while gets
print if /start/../end/
end
Until 'start' is entered in, the entire expression is false, and nothing is printed.
When 'start' is input, the entire expression is true, therefore EVERYTHING input after this point will also be printed out. (despite not being 'start')
As soon as 'end' is input, the entire expression evaluates to false, and nothing from that point on is printed out.
It's called the flip-flop operator. You can read more at "Ruby flip-flop operator".

Regex to capture a stored procedure definition

I have a file with stored procedures defined that look like:
CREATE PROCEDURE [XXXX].[procedure_name_here]
My regex so far is:
\.\[.*\]+.*
reference: http://rubular.com/r/Z0FiI78bqF
I need some help as it doesn't seem to be 100% correct.
Note: the [xxxx]. part may or may not be present (optional), but the word CREATE has to be there otherwise it would be another stored procedure calling a stored procedure, I'm just looking for the actual definition.
If you can rely on the procedure name being between square brackets immediately after a dot, then you can write
\.\[(.*?)\]
the parentheses will capture the procedure name string for you.
If the dot is optional, or you need more verification that the line is a procedure definition, then use
CREATE\s+PROCEDURE\s+(?:\[.*?\]\.)?\[(.*?)\]
SQL is not a regular language. That means it cannot correctly be parsed with a regular expression. It is certainly possible to write a correct parser for SQL, and it is certainly possible to write a correct parser for the subset of SQL that you care about. But it is not possible to do so with only a regular expression.
You can use regular expressions as a guess. But, depending on the choice of regular expression, you will get both false positives, where the regular expression says some text is valid SQL when it's not, and false negatives, where the regular expression says some text is invalid as SQL when it is actually perfectly valid.
So you are left with: either write a correct parser for SQL, or write a regular expression that makes a best guess - and make sure all your SQL files are written to pass that regular expression.

Is it possible to use Column Properties in Expressions in Powerbuilder?

Say I have a field on a datawindow that is the value of a database column ("Insert > Column). It has conditions in which it needs to be protected (Properties>General>Protect).
I want to have the field background grey when it's protect. At the moment, the only way I can work out how to do this is to copy the protect conditional, no matter how complex, substituting the 1 (protect) and 0 (not protect) for colour values.
Is there some sort of syntax I can use in the Expression field for the column's background colour that references the protect value of the column? I tried
if (column.protect=1, Grey, White)
but it returns errorous saying it expects a TRUE/FALSE condition.
Is what I'm after impossible, or is it just a matter of getting the right syntax.
Cheers.
Wow. You like complex, layered questions.
The first problem is accessing the value, which isn't done as directly as you described. As a matter of fact, you use a Describe() to get the value. The only problem with that is that it comes back as a string in the following format, with quotes around (note that we're using standard PowerScript string notation where ~t is a tab)
"<DefaultValue>~t<Expression>"
You want the expression, so you'll have to parse it out, dropping the quotes as well.
Once you've got the expression, you'll need to evaluate it for the given row. That can be done with another Describe () call, particularly:
Describe ("Evaluate('<expression>', <rownum>)")
The row number that an expression is being evaluated on can be had with the GetRow() function.
This may sound like it needs PowerScript and some interim value storage, but as long as you're willing to make redundant function calls to get a given value more than once, you can do this in an expression, something like (for an example column b):
if (Describe ("Evaluate (~"" + Mid (Describe ("b.protect"),
Pos (Describe ("b.protect"), "~t")+1,
Len (Describe ("b.protect")) - Pos (Describe ("b.protect"), "~t") - 1)
+ "~", " + String (GetRow()) + ")")='1',
rgb(128, 128, 128),
rgb(255,255,255))
This looks complex, but if you put the Mid() expression in a compute field so you can see the result, you'll see that simply parses out the Protect expression and puts it into the Describe (Evaluate()) syntax described above.
I have put one cheat into my code for simplicity. I used the knowledge that I only had single quotes in my Protect expression, and chose to put the Evaluate() expression string in double quotes. If I was trying to do this generically for any column, and couldn't assume an absence of double quotes in my Protect expression, I'd have use a global function to do a replace of any double quotes in the Protect expression with escaped quotes (~"), which I believe in your code would look like a triple tilde and a quote. Then again, if I had to make a global function call (note that global function calls in expressions can have a significant performance impact if there are a lot of rows), I'd just pass it the Describe ("column.protect") and GetRow() and build the entire expression in PowerScript, which would be easier to understand and maintain.
Good luck,
Terry.

Resources