What is the "select when" syntax for? - crystal-lang

Experimenting with the language I've found that select is defined in the global scope and its precedence is higher than local variables.
def example(select)
puts select
end
example 3
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
So experimenting with select step by step I get this:
select 1 end
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
and then
select when 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x = 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x
# Syntax error in eval:1: unexpected token: EOF (expecting ',', ';' or '
I'll skip ahead a few steps as you should have an idea of how I've come to my question…
select when x;
else y
end
# Error in line 1: undefined local variable or method 'x_select_action'
and lastly
x_select_action = 4
select when x;
else y
end
# Error in line 3: undefined method 'x_select_action' (If you declared 'x_select_action' in a suffix if, declare it in a regular if for this to work. If the variable was declared in a macro it's not visible outside it)
So there is this keyword in the language which precedes local variables precedence and I don't know what it's for. But apparently it looks for x_select_action when x is given as a when clause. What is this select for and how is it meant to be used?
Searching online I see select defined on Enumerable, Hash, Channel, and Array… but at first glance these don't seem to be it.
Thanks for the help!

It's similar to Go's select: https://tour.golang.org/concurrency/5
But it still needs some tweaks to be finished, that's why there are no docs about it yet.

Related

Pine Script V5 getting syntax error when checking boolean value with 'If' statement

Trying to detect 3 specific EMA crosses, assign them to boolean variable, plot them and set alerts when their conditions are true.
No problem detecting and plotting the 3 crosses when they occur but when I try to set alerts using 'If' statement to check boolean value = true, I get a syntax error at the if statement.
Error is :
Syntax error at input 'end of line without line continuation'
I've tried a thousand different ways to get rid of the syntax error. How am I so stumped on a simple syntax error? Lol I need a fresh set of eyes to look at this for me. Maybe its obvious?
Code:
Continuationema = ta.crossover(ema2_, ema8_)
plotshape(Continuationema, style=shape.triangleup, color=color.new(color.blue, 0), location=location.abovebar)
Bearema = ta.crossunder(ema1_, ema2_)
plotshape(Bearema, style=shape.triangledown, color=color.new(color.red, 0), location=location.abovebar)
Bullema = ta.crossover(ema2_, ema3_)
plotshape(Bullema, style=shape.triangleup, color=color.new(color.green, 0), location=location.belowbar)
// Set Alerts
If Continuationema
strategy.entry("Continuation",strategy.long,alert_message="Uptrend Detected")
alert("Uptrend Continuation Detected. EMA2 Crossing Over EMA8")
*** Syntax error at input 'end of line without line continuation'*** appears at the above 'If' statement
It is you IF statement wich is not well written, you should not use Capital ( If ) but :
if Continuationema == true

oracle.jdbc.OracleDatabaseException: ORA-01722: invalid number

Getting the above error but the offending column is not a number. Somehow it is related to the cycle_code column which is a string. My query is hacky since I dont have full control over building it so I have to append another condition at the end ( "!= 'ABC'"). If I remove that condition the query works. I dont understand why im getting an invalid number error though?
select * from my_view WHERE ... CYCLE_CODE IN ( 'XYZ' ) AND CYCLE_CODE != 'ABC';

How to fix errors in my Fortran code

I am not familiar with errors in Fortran. Could you please explain what are these errors?
int.f90(18): error #5082: Syntax error, found IDENTIFIER 'K' when expecting one of: ( % : . = =>
do k=0.0 to m
---^
int.f90(19): error #5082: Syntax error, found IDENTIFIER 'Q' when expecting one of: ( % : . = =>
do q=1 to m-1
----^
int.f90(20): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ) ,
f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
----------------------------------------------^
int.f90(24): error #5082: Syntax error, found IDENTIFIER 'I' when expecting one of: ( % : . = =>
do i=1 to m-1
----^
int.f90(4): error #6406: Conflicting attributes or multiple declaration of name. [INT]
function int(f,a,b,int,m)
-------------------^
int.f90(9): error #6418: This name has already been assigned a data type. [M]
integer:: i,m
------------^
int.f90(10): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [PI]
real,parameter:: pi,pi=3.14
-----------------^
int.f90(10): error #6418: This name has already been assigned a data type. [PI]
real,parameter:: pi,pi=3.14
--------------------^
int.f90(11): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [EPS]
real,parameter:: eps,eps=1.89
-----------------^
int.f90(11): error #6418: This name has already been assigned a data type. [EPS]
real,parameter:: eps,eps=1.89
---------------------^
int.f90(12): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [E]
real,parameter:: e,e=1.602*((10)**(-19))
-----------------^
int.f90(12): error #6418: This name has already been assigned a data type. [E]
real,parameter:: e,e=1.602*((10)**(-19))
-------------------^
int.f90(21): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
int.f90(23): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
int.f90(26): error #6410: This name has not been declared as an array or a function. [F2]
s=2*f2(t)+4*f2(t+h)
-----^
int.f90(27): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
and this is my code:
function int(f,a,b,int,m)
implicite none
double precision f1,f2,a,b,m,int,s
double precision h,t
integer:: k,q
integer:: i,m
real,parameter:: pi,pi=3.14
real,parameter:: eps,eps=1.89
real,parameter:: e,e=1.602*((10)**(-19))
a=0.0
b=pi
m=150
s=0.0
h=(b-a)/m
do k=0.0 to m
do q=1 to m-1
f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
end do
f2=((2*pi*(e**2))/eps)*f
end do
do i=1 to m-1
t=a+(i*h)
s=2*f2(t)+4*f2(t+h)
end do
int=(h/3)*(s+f2(a)+f2(b)+4*f2(a+h))
print*,int
return
end function int
Your errors are:
Your loop-control in every one of your do loops is malformed. (Fortran 2008 Cl. 8.1.6.2 R818)
DO loops are specified as (neglecting optional syntax):
do variable=start, end
Note the comma rather than the word to.
Parenthesis must be balanced. (See Fortran 2008 7.1.2.2 R701 ( expr ))
Every left parenthesis ( must have a corresponding right parenthesis ). Your statements assigning f1 and f2 both have more left parenthesis than right parenthesis.
Declaring a variable twice in the same scoping unit is wrong. (Fortran 2008 Cl. 16.3.1 paragraph 3, see also: Cl. 5.2)
To delcare a variable and initialize it, you need only use its name once
real, parameter :: pi=3.14
You call the function int and a dummy variable int. You need to use different names for these.
You declare m as both integer and double precision. You can only declare it as one type, not two.
Of note:
Fix errors starting with the first ones, as later ones can be caused by the compiler throwing out earlier bad statements and will go away when those are fixed.
Real values in do loop variables are deleted in the latest standard, and should be avoided in new code. (Fortran 2008 Annex B.1 2(1)).
Your code demonstrates a lack of understanding in Fortran and you will benefit greatly from finding a Fortran tutorial that covers the basics of variables and loops.

gst regular expression mismatch of group generates exception

I have a simple example in GNU Smalltalk 3.2.5 of attempting to group match on a key value setting:
st> m := 'a=b' =~ '(.*?)=(.*)'
MatchingRegexResults:'a=b'('a','b')
The above example works just as expected. However, if there is no match to the second group (.*), an exception is generated:
st> m := 'a=' =~ '(.*?)=(.*)'
Object: Interval new "<-0x4ce2bdf0>" error: Invalid index 1: index out of range
SystemExceptions.IndexOutOfRange(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.IndexOutOfRange class>>signalOn:withIndex: (SysExcept.st:660)
Interval>>first (Interval.st:245)
Kernel.MatchingRegexResults>>at: (Regex.st:382)
Kernel.MatchingRegexResults>>printOn: (Regex.st:305)
Kernel.MatchingRegexResults(Object)>>printString (Object.st:534)
Kernel.MatchingRegexResults(Object)>>printNl (Object.st:571)
I don't understand this behavior. I would have expected the result to be ('a', nil) and that m at: 2 to be nil. I tried a different approach as follows:
st> 'a=' =~ '(.*?)=(.*)' ifMatched: [ :m | 'foo' printNl ]
'foo'
'foo'
Which determines properly that there's a match to the regex. But I still can't check if a specific group is nil:
st> 'a=' =~ '(.*?)=(.*)' ifMatched: [ :m | (m at: 2) ifNotNil: [ (m at: 2) printNl ] ]
Object: Interval new "<-0x4ce81b58>" error: Invalid index 1: index out of range
SystemExceptions.IndexOutOfRange(Exception)>>signal (ExcHandling.st:254)
SystemExceptions.IndexOutOfRange class>>signalOn:withIndex: (SysExcept.st:660)
Interval>>first (Interval.st:245)
Kernel.MatchingRegexResults>>at: (Regex.st:382)
optimized [] in UndefinedObject>>executeStatements (a String:1)
Kernel.MatchingRegexResults>>ifNotMatched:ifMatched: (Regex.st:322)
Kernel.MatchingRegexResults(RegexResults)>>ifMatched: (Regex.st:188)
UndefinedObject>>executeStatements (a String:1)
nil
st>
I don't understand this behavior. I would have expected the result to be ('a', nil) and that m at: 2 to be nil. At least that's the way it works in any other language I've used regex in. This makes me think maybe I'm not doing something correct with my syntax.
My question this is: do I have the correct syntax for attempting to match ASCII key value pairs like this (for example, in parsing environment settings)? And if I do, why is an exception being generated, or is there a way I can have it provide a result that I can check without generating an exception?
I found a related issue reported at gnu.org from Dec 2013 with no responses.
The issue had been fixed in master after the above report was received. The commit can be seen here. A stable release is currently blocked by the glib event loop integration.
ValidationExpression="[0-9]{2}[(a-z)(A-Z)]{5}\d{4}[(a-z)(A-Z)]{1}\d{1}Z\d{1}"
SetFocusOnError="true" ControlToValidate="txtGST" Display="Dynamic" runat="server" ErrorMessage="Invalid GST No." ValidationGroup="Add" ForeColor="Red"></asp:RegularExpressionValidator>

Stata: Ado file syntax

I am working with an .ado file named flow. If the user types flow i I want one if statement to run. If the user types flow e, I want another if statement to run.
How do I do this?
Many readers of this forum expect to see some code that you tried....
program flow
version 8 // will work on almost all Stata in current use
gettoken what garbage : 0
if "`what'" == "" | "`garbage'" != "" | !inlist("`what'", "e", "i") {
di as err "syntax is flow e or flow i"
exit 198
}
if "`what'" == "e" {
<code for e>
}
else if "`what'" == "i" {
<code for i>
}
end
The last if condition is redundant as we've already established that the user typed e or i. Edit it out according to taste.
Given your comment on the answer by #NickCox, I assume you tried something like this:
program flow
version 8
syntax [, i e]
if "`i'`e'" == "" {
di as err "either the i or the e option needs to be specified"
exit 198
}
if "`i'" != "" & "`e'" != "" {
di as err "the i and e options cannot be specified together"
exit 198
}
if "`e'" != "" {
<code for e>
}
if "`i'" != "" {
<code for i>
}
end
After that you call flow like this: flow, i or flow, e. Notice the comma, this is now necessary (but not in the command by #NickCox) because you made them options.
If you want i and e to be mutually exclusive options, then this is yet another alternative:
program flow
version 8
capture syntax , e
if _rc == 0 { // syntax matched what was typed
<code for e>
}
else {
syntax , i // error message and program exit if syntax is incorrect
<code for i>
}
end
If the code in each branch is at all long, many would prefer subprograms for each case as a matter of good style, but that would be consistent with the sketch here. Note that in each syntax statement the option is declared compulsory.
The effect of capture is this: errors are not fatal, but are "eaten" by capture. So you need to look at the return code, accessible in _rc. 0 for _rc always means that the command was successful. Non-zero always means that the command was unsuccessful. Here, and often elsewhere, there are only two ways for the command to be right, so we don't need to know what _rc was; we just need to check for the other legal syntax.
Note that even my two answers here differ in style on whether a user typing an illegal command gets an informative error message or just "invalid syntax". The context to this is an expectation that every Stata command comes with a help file. Some programmers write on the assumption that the help file explains the syntax; others want their error messages to be as helpful as possible.