In Javascript I can declare a sparse array like:
a = [, 1]
But that gives an error in Coffeescript. So how can I create sparse arrays in Coffeescript?
I want to do it in a single assignment and not like:
a = []
a[1] = 1
Probably not possible.
But you can always embed javascript:
`a = [, 1]`
Related
In short, with Swift I can declare an empty Array of a particular type with something akin to the following (without having to use the initializer syntax):
var myArray : [Int] = []
I have not been able to find a similar shorthand to declare an empty Set since they both use the brackets. I either have to use the initializer syntax or I have to use:
var mySet : Set<Int> = []
Which, granted, is not odious, but I wondered if I am missing some syntax sugar somewhere.
(added) "Shorthand" was a bad choice of words. What I was really after was clarity, I guess. In the example for myArray, it's pretty clear what is going on, and it matches the format for a lot of other common variable declarations. The angle <> notation is a bit more obscure.
There is in fact no such syntax in the current (5.x) version of Swift.
I am not sure what you mean by without having to use the initializer syntax
var mySet = Set<Int>() is my preferred shorthand
use this
var set1 = Set<Int>()
set1 = [10,20,30]
print("set \(set1)") // [10,20,30]
set1 = []
print("set \(set1)") // []
I have a 2d array and I want to check whether an array exists inside the 2d array.
I have tried:
var arr = Array(Array(2,1), Array(4,3))
var contain = arr.contains(Array(4, 3))
println(contain)
This should print true but it doesn't work.
Method contains doesn't work because it uses equals to determine equality and for arrays equals is using reference equality, so it will return true only for two references pointing the same object.
You could use find + sameElements:
var arr = Array(Array(2,1), Array(4,3))
var contain = arr.find(_.sameElements(Array(4, 3))).isDefined
println(contain)
Consider using ArrayBuffer instead of Array, if you need mutable collection, like so
val arr = ArrayBuffer(ArrayBuffer(2,1), ArrayBuffer(4,3))
val contain = arr.contains(ArrayBuffer(4, 3))
println(contain)
which outputs
true
Also consider question What is the difference between ArrayBuffer and Array
A more elegant solution would be the following
val array = Array(Array(2,1), Array(4,3))
val result = array.exists(_.sameElements(Array(4, 3)))
println(result)
Output
true
I have a 3 dimensional array. I want to set three elements of it like this:
$array[$x][$y][0 .. 2] = (0, 1, 2);
but perl tells me:
Useless use of a constant (1) in void context
In array context:
#array[$x][$y][0 .. 2] = (0, 1, 2);
but perl tells me:
syntax error near "]["
presumably meaning that it expects me to give it two indices and then assign to the third dimension as a separate array? However, on this page, under Example: Assignment Using Array Slices, it suggests that it is possible to assign to a slice using the range operator where it says:
#array1[1..3] = #array2[23..25];
How can I assign to a slice of the array like this, or do I have to assign each index individually?
You need to dereference the inner array:
#{ $arr[$x][$y] }[ 0 .. 2 ] = (0, 1, 2);
$array[$x][$y][0..2] isn't a slice; it's just an element lookup.
When you attempted to change it into a slice, you sliced the wrong array. You sliced #arr instead of #{ $arr[$x][$y] }.
The key here is to realize that there's no such thing as 3d arrays in Perl. What you have is an array of references to arrays of references to array, which is colloquially called array of array of array, and often abbreviated to AoAoA.
Array slices have the following syntax:
#NAME[LIST]
#BLOCK[LIST]
#$REF[LIST]
EXPR->#[LIST][1]
You could use any of the following:
The first syntax can't be used since the array to slice doesn't have a name.
#{ $array[$x][$y] }[0..2] = 0..2;
my $ref = $array[$x][$y]; #$ref[0..2] = 0..2;
$array[$x][$y]->#[0..2] = 0..2;[1]
See Dereferencing Syntax.
Requires Perl 5.24+. Available in Perl 5.20+ by adding both use feature qw( postderef ); and no warnings qw( experimental::postderef );.
According to the Matlab R2016a documentation, a symbolic multidimensional array can be comfortably created by using the sym command as follows:
A = sym('a',[2 2 2])
and the output is
A(:,:,1) =
[ a1_1_1, a1_2_1;
a2_1_1, a2_2_1]
A(:,:,2) =
[ a1_1_2, a1_2_2;
a2_1_2, a2_2_2]
However, I'm using Matlab 2013b and this command doesn't work for multiple dimensions. Is there any other way to create such variables for the 2013b version?
I'm not yet using R2016a, but looking around the code for the sym class (type edit sym in your Command Window), it's not too hard to write one's own function to do this:
function s = ndSym(x,a)
a = a(:).';
format = repmat('%d_',[1 numel(a)]);
x = [x format(1:end-1)];
s = cellfun(#createCharArrayElement,num2cell(1:prod(a)),'UniformOutput',false);
s = sym(reshape(s,a));
function s = createCharArrayElement(k)
[v{1:numel(a)}] = ind2sub(a,k);
s = sprintf(x,v{:});
end
end
You can the test it via A = ndSym('A',[2 2 2]), which returns:
A(:,:,1) =
[ A1_1_1, A1_2_1]
[ A2_1_1, A2_2_1]
A(:,:,2) =
[ A1_1_2, A1_2_2]
[ A2_1_2, A2_2_2]
This function should work for arrays with an arbitrary number of dimensions. I tested it in R2013b and R2015b. However, note that the function above doesn't incorporate any input validation and many of the options/niceties supported by sym. These could be added. Also, be aware that many pre-R2016a symbolic math functions may not support such multi-dimensional arrays.
Just started using Swift and I'm getting pissed at a few elements. First is that most standard stuff are structs rather than objects, which means they're passed in as values rather than pointers as I'm used to. The other thing is that using the optional element system is really annoying.
If I am trying to declare an array without putting anything in it, I declare it like this:
var theArray : [Int]
In order to put anything in it, I would declare it like this:
var theArray : [Int]?
Then add objects as follows:
theArray[someIndex] = someInt
//or
theArray.append(someInt)
However, I get an error. In Java, I could have just initialized an array with a length, which would have given me an a fixed-size array with all 0's.
The problem, summarized in a sentence, is adding elements to Swift arrays that have been initialized without values. How do you do this?
In order to initialize an empty array use:
var theArray : [Int] = []
then add elements by using append method. What you currently did is that you just declared it in the first case non optional and in the second case as an optional variable typed as int array without initializing it.
If you want the array to contain Int types, of course there are many ways to declare that, based on implicit or explicit type inference. These are all valid declarations of an array containing Int types
var array1 = [Int]()
var array2: [Int] = []
var array3 = Array<Int>()
var array4: Array<Int> = []
If you want an array of a certain size, with the values initialized to a certain value you can use, in this example you'll get an Array<Int> with 5 elements, all initialised to 0
var array = Array(count: 5, repeatedValue: Int(0))
Here you go:
var theArray = Array(count:[the length you want], repeatedValue:Int(0))
This will replicate the Java behaviour.