Are there options for converting an Array to a Vector in ActionScript without iterating the array?
What about the other way (converting a Vector to an Array)?
For the Array to Vector, use the Vector.<TYPE>() function that accept an array and will return the vector created :
var aObjects:Array = [{a:'1'}, {b:'2'}, {c:'3'}];
// use Vector function
var vObjects:Vector.<Object> = Vector.<Object>(aObjects);
For the other there is no builtin function, so you have to do a loop over each Vector item and put then into an Array
vObjects.push.apply(null, aObjects);
And another way to do it.
The trick here is simple. If you try to use the concat() method to load your array into a vector it will fail because the input is a vector and instead of adding the vector elements AS will add the whole vector as one entry. And if you were to use push() you'd have to go through all items in the array and add them one by one.
In ActionScript every function can be called in three ways:
Normal way: vObjects.push(aObjects)
Throws error because aObjects is not an Object but an Array.
The call method: vObjects.push.call(this, myObject1, myObject2, ..., myObjectN)
Doesn't help us because we cannot split the aObjects array into a comma-separated list that we can pass to the function.
The apply method: vObjects.push.apply(this, aObjects)
Going this route AS will happily accept the array as input and add its elements to the vector. You will still get a runtime error if the element types of the array and the vector do not match. Note the first parameter: it defines what is scoped to this when running the function, in most cases null will be fine, but if you use the this keyword in the function to call you should pass something other than null.
var myArray:Array = [].concat(myVector);
might work.
Related
I have a fixed-size array I want to get the last element:
let array = [1, 2, 3];
According to the documentation page of the fixed-size array, there is no last method directly implemented for that type.
After a quick research, I have noticed the slice type implements the last method. All I have to do now is to find a way to convert my array to a slice. I can do that using the iter method:
let last = array.iter().last();
But I also have noted that I can omit the call to iter and get the exactly same behavior:
let last = array.last();
Why is this possible? How can the fixed-size array type call a method it doesn't implement? I have taken a look to all the traits implemented for that type and none of them have a last method.
While I was writing this question, I also noticed the iter function is not defined for the fixed-size array type. Is the documentation partial? Or am I bad at reading it?
This is a special case for the array-types, which coerce into slices of the same type automatically. The docs have (only) a small sentence about this:
Arrays coerce to slices ([T]), so a slice method may be called on an
array.
I both cases the array is coerced into a slice, so you end up calling .iter() and .last() on an implicit slice, not on the array.
i have the code below :
if encoded(i)==code1(2,j)
that is a simple line and basically i want to save a specific element from a cell array named code1 into another array named encoded. The problem here is that matlab shows the below message:
Undefined operator == for input arguments of type cell.
Is there any solution to this problem? How can i save an element from a cell array into a normal array in MATLAB?
Use the function cellfun. Depending on the type of data in code1{2,j}, the first argument of the cellfun should be an appropriate function.
For example, if code1{2,j} is a string, use
cellfun(#str2double, code1{2,j})
Alternatively, if code1{2,j} is an array, use cell2mat as
cell2mat(code1{2,j})
I have a cell array in MATLAB, say A{1}, A{2}, A{3},...., A{561}.
I want pass it to a function argument like:
horzcat(A{1}, A{2}, ..., A{561})
Obviously, it is a lethargic way to write all the cells. What is the shortcut way to do it?
I have already tried horzcat(A{1}:A{561}) but it is not working.
Convert your cell array A into a comma-separated list using A{:} and then pass it as a function input argument e.g.
horzcat(A{:})
I have a some high dimensional arrays and want to apply some functions to them. The problem is I cannot tell apply() (or similar in apply family) that the object to pass the function on is not in the first argument of the function.
Dumb example here:
data <- c(1,2,3,4,5)
myarray <- array(data,dim=c(5,5,2,2),dimnames=list(NULL,
paste("col",1:5,sep=""),
paste("matrix",1:2,sep=""),
paste("slice",1:2,sep="")))
Now, imagine a function with this structure:
function(arg1,arg2,arg3,data,arg4)
If I want to make apply pass the function to the object "myarray", I need to specify on which argument ("data") is located. I tried this but with no results:
apply(myarray,c(3,4),function,arg1,arg2,arg3,arg4)
Thanks in advance!
If I have understood your right, you need to specify the non iterating arguments. For example:
func <- function(a,b,c){
return(a*b + c)
}
apply(FUN=func,a=10,b=10,someObject)
The non specified argument is the argument that is iterated over with your specified vector.
Note that if you have a 1D structure
unlist(lapply(FUN=func,a=10,b=10,someObject))
Will likely work better.
What is the most efficient way to empty an Array or Vector in ActionScript 3?
I've always just re-initialized them:
vector = new Vector.<T>();
array = [];
It doesn't look like there's an empty() function or anything similar.
Is there a better way?
Re-initializing the array is fine in most cases, since the garbage collector will just sweep up the old array. Still, if you want to empty an array without creating a new one you can set array.length = 0
Another option is to use the splice method.
Array::splice documentation
For an array the following call empties it:
array.splice(0);
For a vector the second parameter is enforced, so the call becomes:
vector.splice(0, vector.length);