Re: [Factor-talk] Mutation on tuple instances

2015-11-26 Thread Jon Harper
Ah there it is: http://docs.factorcode.org/content/article-syntax-literals.html
"
If a quotation contains a literal object, the same literal object
instance is used each time the quotation executes; that is, literals
are “live”.

Using mutable object literals in word definitions requires care, since
if those objects are mutated, the actual word definition will be
changed, which is in most cases not what you would expect. Literals
should be cloned before being passed to a word which may potentially
mutate them.
"
Jon


On Thu, Nov 26, 2015 at 5:50 PM, Jon Harper  wrote:
> On Thu, Nov 26, 2015 at 5:28 PM, Sankaranarayanan Viswanathan
>  wrote:
>> Hi,
>>
>> I'm having trouble understanding the behavior below:
>>
>> Running the code below on the listener:
>>
>> TUPLE: test a b c ;
>> :  ( -- t ) { 1 2 } { 3 4 } { 5 6 } test boa ;
>> : f1 ( -- )  . ;
>> : f2 ( -- )  a>> [ [ 10 0 ] dip set-nth ] [ [ 20 1 ] dip set-nth ] bi ;
>>
>> f1
>> ! outputs T{ test { a { 1 2 } } { b { 3 4 } } { c { 5 6 } } }
>>
>> f2 f1
>> ! outputs T{ test { a { 10 20 } } { b { 3 4 } } { c { 5 6 } } }
>>
>> Why is the mutation on a separate tuple instance impacting another?
> The mutation on the second tuple is "affecting" the first tuple
> because they both have references to the same arrays.
>
>> Am I doing something incorrectly?
> When you write "{ 1 2 }" in the source code, only one array is
> created. The idiom if you need separate objects each time the function
> runs is to use "{ 1 2 } clone".
>
> I didn't find where this is described with a quick search in the docs
> , but one place where this is mentioned is at the end of the vector
> article http://docs.factorcode.org/content/article-vectors.html
> "
> If you don't care about initial capacity, an elegant way to create a
> new vector is to write:
> V{ } clone
> "
>
> Cheers,
> Jon

--
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


Re: [Factor-talk] Mutation on tuple instances

2015-11-26 Thread Jon Harper
On Thu, Nov 26, 2015 at 5:28 PM, Sankaranarayanan Viswanathan
 wrote:
> Hi,
>
> I'm having trouble understanding the behavior below:
>
> Running the code below on the listener:
>
> TUPLE: test a b c ;
> :  ( -- t ) { 1 2 } { 3 4 } { 5 6 } test boa ;
> : f1 ( -- )  . ;
> : f2 ( -- )  a>> [ [ 10 0 ] dip set-nth ] [ [ 20 1 ] dip set-nth ] bi ;
>
> f1
> ! outputs T{ test { a { 1 2 } } { b { 3 4 } } { c { 5 6 } } }
>
> f2 f1
> ! outputs T{ test { a { 10 20 } } { b { 3 4 } } { c { 5 6 } } }
>
> Why is the mutation on a separate tuple instance impacting another?
The mutation on the second tuple is "affecting" the first tuple
because they both have references to the same arrays.

> Am I doing something incorrectly?
When you write "{ 1 2 }" in the source code, only one array is
created. The idiom if you need separate objects each time the function
runs is to use "{ 1 2 } clone".

I didn't find where this is described with a quick search in the docs
, but one place where this is mentioned is at the end of the vector
article http://docs.factorcode.org/content/article-vectors.html
"
If you don't care about initial capacity, an elegant way to create a
new vector is to write:
V{ } clone
"

Cheers,
Jon

--
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk


[Factor-talk] Mutation on tuple instances

2015-11-26 Thread Sankaranarayanan Viswanathan
Hi,

I'm having trouble understanding the behavior below:

Running the code below on the listener:

TUPLE: test a b c ;
:  ( -- t ) { 1 2 } { 3 4 } { 5 6 } test boa ;
: f1 ( -- )  . ;
: f2 ( -- )  a>> [ [ 10 0 ] dip set-nth ] [ [ 20 1 ] dip set-nth ] bi ;

f1 
! outputs T{ test { a { 1 2 } } { b { 3 4 } } { c { 5 6 } } }

f2 f1 
! outputs T{ test { a { 10 20 } } { b { 3 4 } } { c { 5 6 } } }

Why is the mutation on a separate tuple instance impacting another?
Am I doing something incorrectly? 


Thanks,
Sankar


--
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
___
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk