This question already has answers here:
Swift Object reference to array?
(4 answers)
Closed 6 years ago.
I found some really strange behavior in Swift. Here's the code:
var array2d: [[Int]] = [[1]]
print(array2d) // prints [[1]]
var first = array2d[0]
first.append(2)
print(array2d) // still prints [[1]]!!!
I would totally expect the last line to print [[1, 2]]. I can't explain the current behavior. I'd expect array2d[0] to return a reference to the first item, or possibly a copy of that reference. In either case, modifying that object should modify array2d. But that's not what's happening.
If, however, I update the array like this:
array2d[0].append(2)
it then prints [[1, 2]], as expected.
Can someone please explain this for me?
How arrays are referenced/passed around/copied in swift is a point of a lot of contention, take a look at this link.
In essence what is happening is that var first = array2d[0] is taking a copy of the array at that index as opposed to creating a reference as you were expecting. Hence accessing the array with the subscript notation allows to you to correctly alter the array but creating a new variable doesn't.
Related
I have a simple code where I create a filled list of integer lists and add an integer to the first element of the list.
List<List<int>> list = List<List<int>>.filled(2, []);
list[0].add(1);
print(list);
What I expect is: [[1],[]]
But in console I get: [[1], [1]]
I can't understand this behavior, can someone explain why this is happening?
You likely meant to use List.generate. The Dart documentation here references this exact situation. By using filled you are explicitly stating that all of the elements have the same fill value.
I'm learning Ruby and have been practicing by solving problems on Codewars and Leetcode. I've come across this problem in Leetcode where it is asking me to, given an array and a value, modify the array in place by removing an occurrence of the value given in the array. Pretty simple! I was able to solve it- but, this curious thing happened and I don't know why!
Here's my code:
def remove_element(nums, val)
nums.each_with_index do |num, index|
if num == val
nums[index] = nil
end
end
nums.compact!
nums.length
end
You can see here that on line 4 I've written "nums[index] = nil", and this worked just fine for me. However, for the longest time I was trying to solve the challenge by writing "num = nil". What doesn't make sense to me is, why does "nums[index]" work and not "num"? Don't they refer to the same thing?
Answer from Dave Newton:
num is a block-local variable, nums is the array. Modifying a local parameter is different than accessing a reference. As another example, say that the array was filled with objects. num.some_property = 5 would modify the property of the array entry, num = SomeNewObject.new would just create a new object and not modify the array entry. Same thing would happen if you were calling a function.
This question already has answers here:
What does map(&:name) mean in Ruby?
(17 answers)
Closed 5 years ago.
I was looking that in Ruby there are a lot of ways to declare arrays objects like:
[1,2,3]
Array.new(3){|i| i+1}
Even with more trickier ways like:
Array.new(3, &1.method(:+))
Where the &1.method(:+) means that the object 1 (reference by value) is responding to the method + directly referenced, so every time (3 times in this case) the array is executing the block, the object increments by 1. I would appreciate any correction if I'm wrong on my analysis here.
So, passing that, there is this case that I'm not completely following:
Array.new(3, &:next)
The question is:
What's the & doing in this case? I can guess that is a reference to some value directly but I don't know which one exactly. And after the &, how the :next is acting in this case.
Beforehand, I really appreciate your help. I'm getting the grasp of Ruby and I'm liking it even more!
& is a shortcut for Symbol#to_proc in this case. A corresponding full version is:
Array.new(3) { |i| i.next }
#=> [1, 2, 3]
The instance below can help you understanding how does it work:
Array.new(3, &(:next.to_proc))
#=> [1, 2, 3]
This question already has answers here:
How to copy end of the Array in swift?
(6 answers)
Closed 6 years ago.
Swift's implementation of arrays is throwing me for a loop again! (Haw, haw.) All I want is to take an array of arbitrary length and get a new array (or an array slice) from it that has the first element removed.
Why is this so hard?
I just want to be able to do something like this:
let suffix = someArray.suffixFrom(1)
But I can't figure out how to do this. It seems the closest method I've found requires knowing the length of the array, which I don't know because it's computed inline and I really really hate having to split such a simple concept up into a bunch of variables and lines.
Elaboration:
I have a string split by colons (:) and I just want to create a Set containing all the :-delimited components excluding the first one.
So that a string "one:two:three" would return a set containing ["two", "three"]. Since I can create a Set from an array, all I really need is to get the suffix of the array, but I don't know how many components there are because it's inline:
return Set(attributeName?.componentsSeparatedByString(":").suffixFrom(1))
Why does Swift make this so hard?
Edit: Before a bunch of you suggest it, I'm well aware I could extend the array class to do this myself, but I'm writing a framework and I don't want to do that, and I also don't want to have to write a bloody utility function to do something so darned simple.
The CollectionType.dropFirst(_:) does exactly what you need, with the exact syntax you're looking for.
let suffix = someArray.dropFirst(1)
This question already has answers here:
What's happening when I use for(i in object) in AS3?
(2 answers)
Closed 8 years ago.
When i try to print the object, it simply print in reverse.
Code:
var marcos:Object = new Object();
marcos.nome = "Marcos";
marcos.ano = 19;
for (var prop in marcos)
{
trace(prop + ":" + " " + marcos[prop]);
}
Output:
ano: 19
nome: Marcos
I had search in the adobe documentation about object and for each but nothing seems ot explain that.
When i try to put more elements the object simply get randomic orders, i really dont know what's going on, if someone could help me i would be grateful.
That's just the way it works with a for in loop and a non-array Object. It's documented on the Adobe website:
The for..in loop iterates through the properties of an object, or the elements of an array. For example, you can use a for..in loop to iterate through the properties of a generic object (object properties are not kept in any particular order, so properties may appear in a seemingly random order).