Here is my 'for' loop:
let size:Int = 64
for var z = 0;z < size;++z{
}
I think the syntax is right, but the compiler always tells me:
is it bug or something?
ps: Xcode version6.1.1
From your screenshot, it looks like you're inserting code in the body of a class directly. You should move it to, e.g., the body of a method. Then it will compile fine.
I would still encourage you to insert spaces after ; and : for readability, however.
Related
Hello everyone I am new to scala and after seeing the do..while syntax which is the following:
do{
<action>
}while(condition)
I have been asked to perform this exercise which consists in predicting the output of a program containing the do..while loop.
var set = Set(1)
do {
set = set + (set.max + 1)
} while (set.sum < 32)
println(set.size)
After execution I get the following error:
end of statement expected but 'do' found
do {
I know that it is possible to convert this loop to while (which is even ideal), however I would like to know if the do..while loop still works in scala if yes, is it a syntax error (Because I searched on the net but I found the same syntax and no mention of this error)? if no, is there a version from which the loop is no longer supported?
You can still use do-while in Scala 2.13.10
https://scastie.scala-lang.org/DmytroMitin/JcGnZS3DRle3jXIUiwkb0A
In Scala 3 you can write do-while in while-do manner using Scala being expression-oriented (i.e. the last expression is what is returned from a block)
while ({
set = set + (set.max + 1)
set.sum < 32
}) {}
https://scastie.scala-lang.org/DmytroMitin/JcGnZS3DRle3jXIUiwkb0A/2
https://docs.scala-lang.org/scala3/reference/dropped-features/do-while.html
This code is originally written in Python 2 and I need to translate it in python 3!
I'm sorry for not sharing enough information:
Also, here's the part where self.D was first assigned:
def __init__(self,instance,transformed,describe1,describe2):
self.D=[]
self.instance=instance
self.transformed=transformed
self.describe1,self.describe2=describe1,describe2
self.describe=self.describe1+', '+self.describe2 if self.describe2 else self.describe1
self.column_num=self.tuple_num=self.view_num=0
self.names=[]
self.types=[]
self.origins=[]
self.features=[]
self.views=[]
self.classify_id=-1
self.classify_num = 1
self.classes=[]
def generateViews(self):
T=map(list,zip(*self.D))
if self.transformed==0:
s= int( self.column_num)
for column_id in range(s):
f = Features(self.names[column_id],self.types[column_id],self.origins[column_id])
#calculate min,max for numerical,temporal
if f.type==Type.numerical or f.type==Type.temporal:
f.min,f.max=min(T[column_id]),max(T[column_id])
if f.min==f.max:
self.types[column_id]=f.type=Type.none
self.features.append(f)
continue
d={}
#calculate distinct,ratio for categorical,temporal
if f.type == Type.categorical or f.type == Type.temporal:
for i in range(self.tuple_num):
print([type(self.D[i]) for i in range(self.tuple_num)])
if self.D[i][column_id] in d:
d[self.D[i][column_id]]+=1
else:
d[self.D[i][column_id]]=1
f.distinct = len(d)
f.ratio = 1.0 * f.distinct / self.tuple_num
f.distinct_values=[(k,d[k]) for k in sorted(d)]
if f.type==Type.temporal:
self.getIntervalBins(f)
self.features.append(f)
TypeError: 'map' object is not subscriptable
The snippet you have given is not enough to solve the problem. The problem lies in self.D which you are trying to subscript using self.D[i]. Please look into your code where self.D is instantiated and make sure that its an array-like variable so that you can subscript it.
Edit
based on your edit, please confirm that whether self.D[i] is also array-like for all i in the range mentioned in the code. you can do that by simply
print([type(self.D[i]) for i in range(self.tuple_num))
share the response of this code, so that I may help further.
Edit-2
As per your comments and the edited code snippet, it seems that self.D is the output of some map function. In python 2, map is a function that returns a list. However, in python3 map is a class that when invoked, creates a map object.
The simplest way to resolve this is the find out the line where self.D was first assigned, and whatever code is in the RHS, wrap it with a list(...) function.
Alternately, just after this line
T=map(list,zip(*self.D))
add the following
self.D = list(self.D)
Hope this will resolve the issue
We don't have quite enough information to answer the question, but in Python 3, generator and map objects are not subscriptable. I think it may be in your
self.D[i]
variable, because you claim that self.D is a list, but it is possible that self.D[i] is a map object.
In your case, to access the indexes, you can convert it to a list:
list(self.D)[i]
Or use unpacking to implicitly convert to a list (this may be more condensed, but remember that explicit is better than implicit):
[*self.D[i]]
I'm currently having issues with Powershell. This is the line that errors:
while($minutes -le ($total_bots/$increment)) {
All of the above variables are numbers. I'm getting an error saying that it can't convert 60/5 - ($total_bots/$increment) - to a int32. I want the math operation completed, and then the condition evaluated. What am I missing?
Variables are declared like so:
[int]$minutes = 1
[int]$increment = 5
[int]$total_bots = 20
I didn't have the int there initially, but it still errors with it.
Was erroring below this after I changed them to [int]. Works now. Thanks
If you run this code, the variable f seems to shadow the function f. Is there anyway to reach the function f?
func f (a:Int)->Int{
return a + 43
}
var f = {(a:Int) in a + 42}
var z = f(1)
println(z)
No.
In Swift, function declarations are simply shortcuts for what you did with that closure + variable thing. That is, function names are essentially constants and should always be viewed as such (you can even pass around the function name, without brackets, as a reference).
What you're doing is you're redeclaring the name f to the variable closure. It seems Swift has a compiler issue not complaining about this. However, this problem would never occur in good code, so it's not a real problem.
It can be a bit confusing, though.
I am very new to EPL queries.
Wrote this and it is throwing syntax error.
#Name('ExpressionTotalQuantitySoFar')
#Description('Gets the total quantity of a symbol so far')
create expression totalQuantitySoFar{ (TAX) =>
(Select sum(T.quantity) from TaxlotWindow as T where T.symbol = TAX.symbol and T.taxlotId < TAX.taxlotId)
};
create variable double totQty = 5.0 ;
#Name('ExpressionLongDebitBalanceTaxlotNoBox')
#Description('Check is if a trade side is invalid, returns rue for invalid statements')
create expression longDebitBalanceTaxlotNoBox{ (SECUR,TAX,ORD,AUE,FX) =>
totQty = totalQuantitySoFar(TAX)
case when (totQty > 0)
then cashImpactBase(SECUR,TAX,ORD,AUE,FX)*(-1)
else
0.0
end
};
It gives syntax error near case.
Any help?
Always include the syntax error text when posting. Else how is one supposed to be able to help.
My tip would be to simplify until the syntax is fine. Then add back stuff.
Most likely this strange declaration "totQty=.." is the cause as its wrong. EPL expressions are not a programming language and don't allow variable declarations like in Java or Scala. Perhaps just use a Java static method to compute instead of you need a programming language.