slang-users mailing list

[2022 Date Index] [2022 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Surprising behavior of array_swap


Hi Jakob,

Jakob Stierhof <jakob.stierhof@xxxxxx> wrote:
> I was struck by a wired behavior of 'array_swap' (I think). Given an
> array and two different indices the function does what it claims to do: 
> swapping the entries and return nothing. However, given the same 
> indices, i.e., 'array_swap(a, 0,0)' does not change the array (as 
> expected), but also leaves the array on the stack.

This is a bug that I will correct later tonight.  Thanks for pointing
it out.

--John

>
>     variable a = [1,2,3];
>     array_swap(a, 0,1);
>
>     print(a);
>     2
>     1
>     3
>
>     variable b = array_swap(a, 0,0);
>     b[0] = 5;
>
>     print(a);
>     5
>     1
>     3
>
> It seems that this is intended, at least according to the comment in 
> slarrfun.c. But I was wondering about the rationale. It makes my 
> Fisher-Yates shuffle two lines longer than should be necessary (or its 
> output very hard to predict) and the (a[i],a[j]) = (a[j],a[i]) a cleaner 
> option.
>
> In case this is not intended I added a patch popping the array 
> regardless of the given arguments.
>
> Thanks,
> Jakob
>
>
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2022 date index] [2022 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]