On Fri, Feb 23, 2007 at 10:49:34AM -0800, Jonathan Lang wrote: : That said, I think I can do one better: : : Ditch all of the above. Instead, '*' always acts like a list of all : valid indices when used in the context of postcircumfix:<[ ]>.
Ooh, shiny! Or at least, shiny on the shiny side... : If you want the last index, say '*[-1]' instead of '* - 1'. : If you want the first index, say '*[0]' instead of '* + 0'. So the generic version of leaving off both ends would be *[1]..*[-2] (ignoring that we'd probably write *[0]^..^*[-1] for that instead). : So the four corners of a two-dimensional array would be: : : @array[ *[0]; *[0] ]; @array[ *[-1]; *[0] ]; : @array[ *[0]; *[-1] ]; @array[ *[-1]; *[-1] ]; A point against it visually is the nested use of []. : The only thing lost here is that '@array[+*]' is unlikely to point : just past the end of a shaped array. But then, one of the points of : shaped arrays is that if you point at an invalid index, you get a : complaint; so I don't see why one would want to knowingly point to : one. I would expect that to point to one off the end in the first dimension only, which might make sense if that dimension is extensible: my @array[*;2;2]; Adding something at +* would then add another 2x2 under it. : Also, has the syntax for accessing an array's shape been determined : yet? If not, I'd like to propose the following: : : @array.shape returns a list of lists, with the top-level list's : indices corresponding to the dimensions of the shape and each nested : list containing every valid index in that dimension. In boolean : context, the shape method returns true if the array is shaped and : false if not - though an unshaped array will otherwise pretend to be a : one-dimensional, zero-based, non-sparse, shaped array. That's more or less how I was thinking of it, though I hadn't got as far as boolean context. : So: : : @array.shape[0][2] # the third valid index of the first dimension of the : shape : @array.shape[-1][0] # the first valid index of the last dimension of the : shape : @array.shape[1] # every valid index of the second dimension of the shape : @array.shape[1][*] # same as @array.shape[1] : : [EMAIL PROTECTED] # is this a shaped array? : : exists @array.shape[2] # does the array have a third dimension? : exists @array.shape[3][4] # does the fourth dimension have a fifth element? : : [EMAIL PROTECTED] # how many dimensions does the shape have? : [EMAIL PROTECTED] # how many indices does the first dimension have? : : If we use this notation, then : : @array[ *; * ] : : is shorthand for : : @array[ @array.shape[0]; @array.shape[1] ] Note also that multidimensional whatever gives us @array[ ** ] to mean @array[ @@( @array.shape[*] ) ] or some such. Though ** might want to be even smarter than that if we want @array[ 0; **; 42] to dwim. That'd have to turn into something like: @array[ 0; @@( @array.shape[*[1]..*[-2]] ); 42 ] Also +** might return a shape vector, or maybe +«**. Larry