Making an independent copy of a list

I would like to make an independent copy of a list so that this isn't true (in actual "real world" memory locations/pointers etc used)

Starting with an easy case

image


People who want to program in Javascript instead of in Snap*!* : This code won't work on non-Snap*!* data structures.

:slight_smile:
You've jumped a bit ahead of my questions :slight_smile:

So my next question is - how can we get it to do that?

So that no matter what the contents of the list are - the copy doesn't share any part of it with the original (except that it has the same values)

I just said that and you should drag the blocks yourself (its just 7 blocks except the blocks it left you when you define a reporter)

I actually mean anything you want to copy but isn't a list like a dictionary

Sorry - I thought you were saying that it wouldn't work for all lists.

As long as it works on any list, then all is good :slight_smile:

thats all right :slight_smile:

ok as i think you wont use it w/ my json lib (that one uses javascript objects)

Simon, there is an easy way for both a shallow copy and a deep copy in Snap!. Let me show you, and please don't let over-eager @18001767679 irritate you, because ... well. Anyway.

To make a shallow copy:

shallow copy

A "shallow copy" means that you'll get a new list structure, but what's inside that result only gets referenced, not duplicated. So, if you shallow-copy a table (a list of rows), you'll get a new table, but each row will be the same as the corresponding one of the source table.

and to make a deep copy:

id

This might be unintuitive at first sight, but it's kinda crazy cool once you see what's happening here, namely that the ID function is a hyperblock.

I'll leave it at this for you to savor.

Beautiful and elegant, ain't it?

(I've edited this post because at first it showed the wrong pic for deep-copy)

Wat?Is there an id block?I've never used it before!Well i know it is in the sqrt of () block

That is what I will do when there is no hyperblocks and no id function

I would love to savour it :slight_smile:

Where can I find it?
[edit] Got it ( I didn't understand what @18001767679 meant !)

image

Exactly!

Yep, nice recursive deep copy!

Both of those are beautiful, and both totally non-intuitive! I should add them to the manual.

This post neatly encapsulates your best and worst attributes as a forum contributor.

The procedure is terrific! My university students have always had trouble wrapping their heads around deep mapping like this. I'm proud of you for inventing it. I think the generalization


is the most beautiful procedure ever!

But then you have to add an incorrect statement of limitation of your code, because

  1. You didn't mean linked lists; you meant putzing around in Javascript, and

  2. You think that putzing around in Javascript is what everyone wants to do in Snap!.

If you have to put irrelevant stuff in the forum, could you please label it as irrelevant? Like this:

People who want to program in Javascript instead of in Snap!: This code won't work on non-Snap! data structures.

Okay? Thanks.