Multidimensional duplication method (about passing by reference)

Another week behind, another challenges done. The levels of difficulty are growing every week now. I’m learning more about Objects / Classes and how to use them in written programs. The whole idea is still new to me, but it feels that I am moving forward, slowly, however.

I am building now Mastermind game. It seems that procedural code would complicate things at this stage. I am making it with three classes and number of instance methods. I am assigning one action to one method. It requires more planning than before when I was just building one huge block of code. I will share one of the finished games soon, but today topic is different.

One of the warmup challenges was to build deep duplication method to Array class. Here is what I have learnt. Let’s follow the below code:

screen-shot-2016-10-11-at-20-39-50We have passed/ assigned values to two variables. var1 hold a sting “house” and this particular string has its house in computer, pigeon hole or allocated a place in memory if you prefer to call it. That place didn’t hold “house” after we have assigned var2 to var1. What happened next is a new place in memory was given to variable var1.. the same location that belongs to var2. I tried to add an extra string in line 006 to var2 – but this operation was not saved anywhere.

I have decided to use shovel operator adding to var2 ” is just too small!”. So var2 is “shed is too small!” Nothing suspicious. However, if we check var1, it has changed as well, it’s not “shed” anymore. This particular location id in computer’s memory explains why this has happened. In fact, when adding the string to var2, we have added a string to the object living under number 70331642254620 in computer’s memory. This is the same location that var1 was referring to. This last verb is important – “referring.” The location in memory is what counts, rather than the name under which we store a string (i.e.,. var1). I did a similar operation on line 011 but the effect is different. The difference is that I have added the string ” is too small to live!” to var2 and assigned to var2. We see now that room number 70331642254620 was not taken by this operation. This assigning process assigned new room number 70331646357140 to var2. The old room is still referred by var1.

It’s easy to imagine that in a big program with many variables, forgetting about the above referencing logic and focusing on values must cause major issues. Having operations affecting an object stored in the same piece of memory may change values of other variables referring to this object id… variables which values shouldn’t have been changed like in the above example.

The solution is dup method – duplication function which creates copy of the same object but assigns different id in memory.
screen-shot-2016-10-11-at-21-06-30Here we have code where duplication breaks this cross reference, so var1 and var2 are two separate objects although having the same value of “shed”.

There is one thing with dup method worth to remember. This nice method doesn’t go too deep. So it can create new id for the same array like: [1, 2, 3] but it won’t for the part [“a”, “b”] in array [1, 2, 3,[“a”, “b”] ].

The example of that can be tracked below:

screen-shot-2016-10-11-at-21-13-48 Here is my turn. The assignment asked to create a method which would duplicate multidimensional array… no matter how many levels of arrays there is within the main array. I got lost in recursion but with a few hints from people in Viking I present below method for duplication of the multidimensional array.

 

screen-shot-2016-10-11-at-21-16-57

I used map method which returns an array, so returns an object with a new id number. Map method iterates through every object of the first level through the array. Here comes recursion (arr1.deep_dup) which takes care of the depth of every object.It works even on beauty like this: deeper_arr = [ [ 1, [ 2, 3 ] ], [ 4, 5, 6], [ [ 7, 8, [ 9, 10 ]], [11, 12] ] ]If we imagine a table if columns and rows, I could say that map iterates through

It works even on beauty like this: deeper_arr = [ [ 1, [ 2, 3 ] ], [ 4, 5, 6], [ [ 7, 8, [ 9, 10 ]], [11, 12] ] ]

If we imagine a table if columns and rows, I could say that map iterates through the top row of these columns… ie. [ 1, [ 2, 3 ] ],   [ 4, 5, 6],   [ [ 7, 8, [ 9, 10 ]] etc. The condition in line 4 checks if the ie. [ 1, [ 2, 3 ] ] is an Array and if it is, it calls the same function deep_dup to get to the next level: 1, [ 2, 3 ], and again iterates though 1 returns it as it is and when it gets to the  [ 2, 3 ] – realizes it’s Array and go deeper again (easier to imagine by the fans of “Inception” or “Primer” I think). Once it gets to 2,3 level, returns them as they are and map method moves on to the next column from top to bottom which will be now   [ 4, 5, 6]. All this saved each time in new memory id which is what map enumerable function does.

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s