Re: Copy A to B, so that no change in A affects B

2016-12-07 Thread Alexander Burger
Hi Danilo,

> `symbols'?  :) Although I can imagine only "tiny" namespaces.

I would say that namespaces have even more overhead.

You might compose the data model as objects (OOP or not), or do surgical (i.e.
destructive) changes to large structures in a locally controlled way.

♪♫ Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Copy A to B, so that no change in A affects B

2016-12-07 Thread Bruno Franco
Thanks Alex, That was just what I needed.
I ended up using the (mapcar copy List) code in some disturbingly
inefficient code. I still
don't know enough to turn it into something that doesn't need it, but my
hope is that
as I work on it I'll find it.

On Wed, Dec 7, 2016 at 4:05 PM, Danilo Kordic 
wrote:

> `symbols'?  :) Although I can imagine only "tiny" namespaces.
>
> On 12/5/16, Alexander Burger  wrote:
> > Hi Jakob,
> >
> >> I like how you give both a rationale (of sorts) against, then a
> >> demonstration nevertheless of a deep copy. Very friendly! :)
> >
> > The main reason against deep copy is efficiency. In a typical application
> > with
> > huge data structures, you will want to change parts of it often. If you
> copy
> > the
> > whole structure each time, you need lots of computing time and processing
> > power.
> >
> > For a demonstration - as I said - I have none ;)
> >
> > Cheers,
> > - Alex
> > --
> > UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
> >
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>


Re: Copy A to B, so that no change in A affects B

2016-12-07 Thread Danilo Kordic
`symbols'?  :) Although I can imagine only "tiny" namespaces.

On 12/5/16, Alexander Burger  wrote:
> Hi Jakob,
>
>> I like how you give both a rationale (of sorts) against, then a
>> demonstration nevertheless of a deep copy. Very friendly! :)
>
> The main reason against deep copy is efficiency. In a typical application
> with
> huge data structures, you will want to change parts of it often. If you copy
> the
> whole structure each time, you need lots of computing time and processing
> power.
>
> For a demonstration - as I said - I have none ;)
>
> Cheers,
> - Alex
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Copy A to B, so that no change in A affects B

2016-12-05 Thread Alexander Burger
Hi Jakob,

> I like how you give both a rationale (of sorts) against, then a
> demonstration nevertheless of a deep copy. Very friendly! :)

The main reason against deep copy is efficiency. In a typical application with
huge data structures, you will want to change parts of it often. If you copy the
whole structure each time, you need lots of computing time and processing power.

For a demonstration - as I said - I have none ;)

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Copy A to B, so that no change in A affects B

2016-12-05 Thread Jakob Eriksson
I like how you give both a rationale (of sorts) against, then a demonstration 
nevertheless of a deep copy. Very friendly! :)

> 5 dec. 2016 kl. 08:43 skrev Alexander Burger :
> 
>> On Mon, Dec 05, 2016 at 01:44:53AM -0500, Bruno Franco wrote:
>> Is there a function that would copy A so that no changes in any of the
>> nested lists in A would change B, and vice versa?
> 
> You mean a "deep copy".
> 
> Contrary to what one might expect, it is very rarely needed. At least I never
> needed it in my 35 years of Lisp. I almost never used even the single-level
> 'copy' function.
> 
> Instead, you would rather re-consider your data structures, and operate on 
> them
> in a non-destructive way.
> 
> 
> Having said this, you can do e.g. a 2-level copy with
> 
>   (mapcar copy List)
> 
> For an arbitrary depth you must use recursion
> 
>   (recur (List)
>  (if (atom List)
> List
> (cons (recurse (car List)) (recurse (cdr List))) ) )
> 
> 
>> Also, as a more general question, is there a name for when two variables
>> are entangled like this? Where changing one changes the other?
> 
> The term is "destructive" operations. In the PicoLisp reference, all 
> destructive
> functions should be marked as that.
> 
> ♪♫ Alex
> -- 
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Copy A to B, so that no change in A affects B

2016-12-04 Thread Alexander Burger
On Mon, Dec 05, 2016 at 01:44:53AM -0500, Bruno Franco wrote:
> Is there a function that would copy A so that no changes in any of the
> nested lists in A would change B, and vice versa?

You mean a "deep copy".

Contrary to what one might expect, it is very rarely needed. At least I never
needed it in my 35 years of Lisp. I almost never used even the single-level
'copy' function.

Instead, you would rather re-consider your data structures, and operate on them
in a non-destructive way.


Having said this, you can do e.g. a 2-level copy with

   (mapcar copy List)

For an arbitrary depth you must use recursion

   (recur (List)
  (if (atom List)
 List
 (cons (recurse (car List)) (recurse (cdr List))) ) )


> Also, as a more general question, is there a name for when two variables
> are entangled like this? Where changing one changes the other?

The term is "destructive" operations. In the PicoLisp reference, all destructive
functions should be marked as that.

♪♫ Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Copy A to B, so that no change in A affects B

2016-12-04 Thread Bruno Franco
I'm trying to copy a list A in a way that changing the lists inside A don't
affect B.
I've tried using 'copy, like this:

: (setq A '((1 2 3) (4 5 6)))
-> ((1 2 3) (4 5 6))
: (setq B (copy A))
-> ((1 2 3) (4 5 6))

And it works at the top level:
#changing B doesn't change A
: (set B 1)
-> 1
: B
-> (1 (4 5 6))
: A
-> ((1 2 3) (4 5 6))

But, as the documentation says, it works only at the top level of the list:
#changing the CAR of B changes the CAR of A
: (setq B (copy A))
-> ((1 2 3) (4 5 6))
: (set (car B) 'a)
-> a
: B
-> ((a 2 3) (4 5 6))
: A
-> ((a 2 3) (4 5 6))

Is there a function that would copy A so that no changes in any of the
nested lists in A would change B, and vice versa?
Also, as a more general question, is there a name for when two variables
are entangled like this? Where changing one changes the other?