Hashset in Nim-lang - hashset

I am trying to use HashSet type of nim-lang but receiving error
var list = initSet\[int]()
and error is
Error: undeclared identifier: 'initSet'
I have already imported hashes library

It's in the sets module, not hashes.
import sets
proc sum(xs: HashSet[int]): int =
for x in xs:
result += x
var list = initHashSet[int]()
list.incl(10)
list.incl(20)
list.incl(30)
echo list.sum

Related

How to create a PyGLM array of float32 type from a list of python floats

I have this function to modify it:
def buffers_init_pnt() -> None:
global mPoints
global POINTS_VAOID
global POINTS_VBOID
global glmverts_pnt_init
floatlist = []
for item in sphere_nodes:
floatlist.append(float(item[1]))
floatlist.append(float(item[2]))
floatlist.append(float(item[3]))
glmverts_pnt_001 = glm.array(glm.float32,
float(item[1]), float(item[2]), float(item[3])
)
mPoints.append(Point(glmverts_pnt_001, POINTS_VAOID, POINTS_VBOID))
buf = struct.pack('%sf' % len(floatlist), *floatlist)
#I would like to create a glm array of float32 type from the list floatlist
#glmtest = glm.array(glm.float32, 0.0, 0.0, 0.0)
#glmtest = glm.array.from_bytes(buf)
#glmtest.dtype = glm.float32
POINTS_VAOID = gl.glGenVertexArrays(1)
POINTS_VBOID = gl.glGenBuffers(1)
gl.glBindVertexArray(POINTS_VAOID)
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, POINTS_VBOID)
gl.glBufferData(gl.GL_ARRAY_BUFFER, glmverts_pnt_init.nbytes, None, gl.GL_DYNAMIC_DRAW)
gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT, gl.GL_FALSE, 3 * glm.sizeof(glm.float32), None)
gl.glEnableVertexAttribArray(0)
I know there is a from_bytes() however I tried to use it and the dtype was showing Uint8 and I couldn't change it since it was write protected. Is there a way to create a glm array from a regular list of python floats?
Create a ctypes array:
import ctypes
floatArray = (ctypes.c_float * len(floatlist))(*floatlist)
This array can be directly used with PyOpenGLs glBufferData overload:
gl.glBufferData(gl.GL_ARRAY_BUFFER, floatArray, gl.GL_DYNAMIC_DRAW)

Heterogeneous collection literal in swift

I am trying to read nested array as follows but getting an error.
var inputArray = [1,[4,3],6,[5,[1,0]]]
func nestedArray(inputArray :[Any])
{
}
error: heterogeneous collection literal could only be inferred to
'[Any]'; add explicit type annotation if this is intentional var
inputArray = [1,[4,3],6,[5,[1,0]]]
You need
var inputArray:[Any] = [1,[4,3],6,[5,[1,0]]]
as you specify elements of different types Int , Array and nested Array

Converting datatypes in Spark/Scala

I have a variable in scala called a which is as below
scala> a
res17: Array[org.apache.spark.sql.Row] = Array([0_42], [big], [baller], [bitch], [shoe] ..)
It is an array of lists which contains a single word.
I would like to convert it to a single array consisting of sequence of strings like shown below
Array[Seq[String]] = Array(WrappedArray(0_42,big,baller,shoe,?,since,eluid.........
Well the reason why I am trying to create an array of single wrapped array is I want to run word2vec model in spark using MLLIB.
The fit() function in this only takes iterable string.
scala> val model = word2vec.fit(b)
<console>:41: error: inferred type arguments [String] do not conform to method fit's type parameter bounds [S <: Iterable[String]]
The sample data you're listing is not an array of lists, but an array of Rows. An array of a single WrappedArray you're trying to create also doesn't seem to serve any meaningful purpose.
If you want to create an array of all the word strings in your Array[Row] data structure, you can simply use a map like in the following:
val df = Seq(
("0_42"), ("big"), ("baller"), ("bitch"), ("shoe"), ("?"), ("since"), ("eliud"), ("win")
).toDF("word")
val a = df.rdd.collect
// a: Array[org.apache.spark.sql.Row] = Array(
// [0_42], [big], [baller], [bitch], [shoe], [?], [since], [eliud], [win]
// )
import org.apache.spark.sql.Row
val b = a.map{ case Row(w: String) => w }
// b: Array[String] = Array(0_42, big, baller, bitch, shoe, ?, since, eliud, win)
[UPDATE]
If you do want to create an array of a single WrappedArray, here's one approach:
val b = Array( a.map{ case Row(w: String) => w }.toSeq )
// b: Array[Seq[String]] = Array(WrappedArray(
// 0_42, big, baller, bitch, shoe, ?, since, eliud, win
// ))
I finally got it working by doing the following
val db=a.map{ case Row(word: String) => word }
val model = word2vec.fit( b.map(l=>Seq(l)))

How to add to an array of objects, an object that is inherited? (Swift, XCode)

var allCards = [CCard]
var cardsMade = 0
^^ is outside of my view controller class as to make it global and accessible in all other view controller files.
in another view controller i have
#IBAction func saveInfo(sender: UIButton) {
let a = CCreature(n: cName!, e: cExpansion, ec: cEnergy!, tc: cTurnCount!,
ef: cEffect!, at: cStrength!, ht:cHealth!, sp: cSpeed!, sb: false)
allCards.append(a)
cardsMade++}
so when the saveInfo button is pressed, i put all the information the user has typed (which were in UITextFields, and then saved into the corresponding vairables cEnergy etc..) into a subclass of CCard called CCreature, with all of the information. After I create the instance of the class, i am trying to store in the array allCards. I am getting an error on the line:
var allCards = [CCard]
Expected member name or constructor call after type name
And in the line where a is appended to the array i am getting this error:
Cannot convert value of type 'CCreature' to expected argument type 'inout Array < CCard >'
I was also getting this error message before, when my program was compiling:
fatal error: Array index out of range
Any ideas?
As you have it [CCard] is a type declaration, you could use it as:
var allCards : [CCard]
but, that won't actually initialize allCards to be useful, alternatively, you could actually create the array and initialize it using:
var allCards = [CCard]()
Where you're using the default array constructor
It's not the entirety of your example, because you didn't include a lot of the pieces, but stripped down to show usage, would be:
var allCards = [CCard]()
func saveInfo() {
let a = CCreature()
allCards.append(a)
}

actionscript 3: how to access to elements of an array created in a loop dynamically

In the library of the .fla file I have a square exported as Class "cuad" on frame 1
I want to create an Array with 100 squares so as to move them later
So I do like this:
for (var i:uint = 0; i<100;i++)
{
var cuad_mc = new cuad();
addChild(cuad_mc);
myArray.push("cuad_mc");
trace(myArray[i]);
}
I have a runtime error
The error you experience is
Error #1069: Did not find alpha propiety in the String and there is not any value predetermined
The problem comes from your line
myArray.push("cuad_mc");
What you are doing here is pushing a String Object into your Array, not the cuad Object you want. String Objects don't have Alpha values, or x values.
What you want to do is
myArray.push(cuad_mc);
cuad_mc (without the " quotation marks) is a reference to the object you just created.
This should solve your problem. I also recommend using Vectors instead of Array if you only need to store one type of Object. Like this:
var myArray:Vector<cuad> = new Vector<cuad>();
for(var i:int=0;i<100;i++){
var cuad_mc:cuad = new cuad();
addChild(cuad_mc);
myArray.push(cuad_mc);
trace(myArray[i]);
}
Vectors are just like Arrays, but they only allow one specific type, so that a situation like yours doesn't occur.

Resources