Re: Anonymous multidimensional array

2009-06-02 Thread John M. Dlugosz

Larry Wall larry-at-wall.org |Perl 6| wrote:

On Tue, Jun 02, 2009 at 08:21:29AM -0700, yary wrote:
: I do see a problem if there's more than one unspecified dimension.
: Though I suppose an array of shape(*;*) as an lvalue might be a
: constraint allowing assignment only of another 2D array?

I don't see why we shouldn't use the capture shape of the value
by default all the time, and do linear reshaping only if the value
comes in as a flat list.  We've gone to some pains to allow ephemeral
shaping of values through captures, and it seems like it's good error
checking to check the shape of the value against the shape of the
container unless explicitly defeated.

That is to say, if you erase the capture shape by putting the value
into list context, it linearizes it, and then the container knows
to reshape.  Otherwise the container attempts to use the value slicily.

Larry

  


Consider:

   (@a,@b...@c) = SomeCapture;

The first container, @a, being fixed-size will grab some of the 
elements.  Now, what's left in SomeCapture is not even starting at a 
top-level boundary in the original structure.  It's in the middle of 
some list some number of levels down.  How does what's left of the 
capture imply the proper shape?


It also means that given a multi-dim array, or a [**] array that can 
hold any morphology, the right-hand-size can't be a normal return from a 
map or whatever because you'll get the structure from _that_.  The shape 
of the list is generally determined by the definitions of function 
return and looping statement semantics, and can't be used to formulate 
the structure you wanted.



As it stands, list assignment is "list context" for the RHS.  That is 
wise.  If the Capture Revolution comes to that, it needs to be carefully 
thought out.


--John


Re: Anonymous multidimensional array

2009-06-02 Thread yary
I haven't gotten deep into the shape/array specs and I need to... nonetheless

On Tue, Jun 2, 2009 at 9:55 AM, Larry Wall  wrote:
> I don't see why we shouldn't use the capture shape of the value
> by default all the time, and do linear reshaping only if the value
> comes in as a flat list.

This highlights an edge case- in my mind at least, a "flat list" has a
different shape from a one-row array. shape(*) or shape() vs
shape(*;0)? Given the above, linear reshaping should not happen when
given an explicitly-1D-shaped array on the right hand side, and that's
something that should be spelled out and included in the spec tests.
Or spell out the contradiction if I guessed wrong!

>We've gone to some pains to allow ephemeral
> shaping of values through captures, and it seems like it's good error
> checking to check the shape of the value against the shape of the
> container unless explicitly defeated.
>
> That is to say, if you erase the capture shape by putting the value
> into list context, it linearizes it, and then the container knows
> to reshape.  Otherwise the container attempts to use the value slicily.

I like this, though my comprehension is currently rather primitive.


Re: Anonymous multidimensional array

2009-06-02 Thread Larry Wall
On Tue, Jun 02, 2009 at 08:21:29AM -0700, yary wrote:
: I do see a problem if there's more than one unspecified dimension.
: Though I suppose an array of shape(*;*) as an lvalue might be a
: constraint allowing assignment only of another 2D array?

I don't see why we shouldn't use the capture shape of the value
by default all the time, and do linear reshaping only if the value
comes in as a flat list.  We've gone to some pains to allow ephemeral
shaping of values through captures, and it seems like it's good error
checking to check the shape of the value against the shape of the
container unless explicitly defeated.

That is to say, if you erase the capture shape by putting the value
into list context, it linearizes it, and then the container knows
to reshape.  Otherwise the container attempts to use the value slicily.

Larry


Re: Anonymous multidimensional array

2009-06-02 Thread yary
On Mon, Jun 1, 2009 at 10:43 PM, John M. Dlugosz > And it should be an
error if dimensions other than the highest are
> unspecified.  How can it know how to shape it?  Use an explicit command to
> shape up the argument in that case.

I don't see why shape(2;*) is not a problem and shape(*;2) is a
problem for an lvalue array, either each one (lazily?) demands an even
number of elements, or they both fail. In general, multiply the size
of the known dimensions, and that's the number of elements you need to
fill up one more in the unspecified dimension direction.

I do see a problem if there's more than one unspecified dimension.
Though I suppose an array of shape(*;*) as an lvalue might be a
constraint allowing assignment only of another 2D array?


Re: Anonymous multidimensional array

2009-06-01 Thread John M. Dlugosz

Larry Wall larry-at-wall.org |Perl 6| wrote:

On Mon, Jun 01, 2009 at 08:23:41PM -0700, yary wrote:
: How does one create an anonymous multidimensional array in p6? Not an
: array of arrays or a capture of captures...

But I would expect a shaped array to be able to coerce either of
those into its internal format.  And coercing captures into
structure is basically what @@/slice context is for.

Larry

  

Right, after meditating on that very thing a few days ago, I conclude

 @A = @list;

should work when one of the "containers" on the left happens to be 
shaped.  The right is flattened, losing any structure it may have had, 
including the properly shaped thing already, a single linear list, or a 
different shape with a suitable number of elements.



I would suggest that the assignment demand sufficient elements to fill, 
or be an integral number of lower-level units if the highest dimension 
is unspecified.  You can always make up the difference by sticking 0 x * 
at the end of the right-hand-side if that was your intent.


And it should be an error if dimensions other than the highest are 
unspecified.  How can it know how to shape it?  Use an explicit command 
to shape up the argument in that case.


Also, if you were using an array of arrays, this will clobber that and 
fill the top array with items.  If the top array is declared to be of 
Arrays, then the assignment will fail just like any other type 
constraint.  But if the second-level arrays are fixed size, you can 
always write |@A on the left to re-fill those from the right hand side.


--John



Re: Anonymous multidimensional array

2009-06-01 Thread Larry Wall
On Mon, Jun 01, 2009 at 08:23:41PM -0700, yary wrote:
: How does one create an anonymous multidimensional array in p6? Not an
: array of arrays or a capture of captures...

But I would expect a shaped array to be able to coerce either of
those into its internal format.  And coercing captures into
structure is basically what @@/slice context is for.

Larry


Anonymous multidimensional array

2009-06-01 Thread yary
How does one create an anonymous multidimensional array in p6? Not an
array of arrays or a capture of captures... I'm guessing it involves
Array.new(:shape) or something like :shape(2;2), and
that it's not yet implemented in Rakudo.

Is anonymous multidimensional array creation covered in the synopses?
Might be good to answer this explicitly in S09, 'less it's in there
and I missed it.

-y