Modified:
doc/trunk/design/syn/S09.pod
Log:
Another idea for *+ vs *- in subscripts
Modified: doc/trunk/design/syn/S09.pod
==============================================================================
--- doc/trunk/design/syn/S09.pod (original)
+++ doc/trunk/design/syn/S09.pod Tue Jan 30 12:20:47 2007
@@ -802,6 +802,12 @@
works...
+Alternately, C<*+0> is the first element, and the subscript dwims
+from the front or back depending on the sign. That would be more
+symmetrical, but makes the idea of C<*> in a subscript a little more
+distant from the notion of "all the keys", which would be a loss,
+and potentially makes C<+*> not mean the number of keys.
+
Conjecture: we might provide a way to declare a modular subscript that
emulates the dwimmery, perhaps by using a subset type:
la...@cvs.develooper.com wrote:
> +Alternately, C<*+0> is the first element, and the subscript dwims
> +from the front or back depending on the sign. That would be more
> +symmetrical, but makes the idea of C<*> in a subscript a little more
> +distant from the notion of "all the keys", which would be a loss,
> +and potentially makes C<+*> not mean the number of keys.
The distinction of the three cases would be through dispatch
to different functions. The plain * goes to
&postcircumfix<[ ]>:(Array,Whatever) while the other two go
to &infix:<+>(Whatever,Int) and &infix:<->:(Whatever,Int)
respectively. In the latter cases the Whatever type somehow must
package the array. Perhaps it is actually Whatever of Array or
somesuch. The return value is a normal Int suitable for indexing
the array. Perhaps we need to restrict the second parameter to
UInt unless these two operators wrap around depending on sign ;)
That is @array[*+(-1)] actually retrieves that last entry and
@array[*-(-1)] the first =:()
Can the index be set up before using it in an array index?
my @a = 1,2,3;
my $x = *-1;
say @a[$x]; # prints 3
Would &infix:<-> return a lazy Whatever value that becomes
a specific index when it hits &postcircumfix<[ ]>? That is
we have something like Whatever arithmetic?
Regards, TSa.
--
I don't think we want Whatever to package the array, since...
: Perhaps it is actually Whatever of Array or
: somesuch. The return value is a normal Int suitable for indexing
: the array. Perhaps we need to restrict the second parameter to
: UInt unless these two operators wrap around depending on sign ;)
: That is @array[*+(-1)] actually retrieves that last entry and
: @array[*-(-1)] the first =:()
:
: Can the index be set up before using it in an array index?
:
: my @a = 1,2,3;
: my $x = *-1;
: say @a[$x]; # prints 3
:
: Would &infix:<-> return a lazy Whatever value that becomes
: a specific index when it hits &postcircumfix<[ ]>? That is
: we have something like Whatever arithmetic?
...I think that should work. The *-1 value just means "one before
Whatever", and the dwim is, as you say, lazy. Or perhaps this generalizes
to an odd form of currying:
&replicate := * xx 42;
But then it's not clear how the eventual victim would know how to
distinguish *+42 from *-42 if it's only getting some kind of closure
to call. So I think we still have to dispatch to Whatever and let
the Whatever code extract any relevant relative information knowing
that a "dwim" was explicitly called for by use of *.
Larry
Larry Wall wrote:
> ...I think that should work. The *-1 value just means "one before
> Whatever", and the dwim is, as you say, lazy. Or perhaps this generalizes
> to an odd form of currying:
>
> &replicate := * xx 42;
Ohh, let me get that in my own words: the rhs replicates a Whatever
into a list of 42 Whatever values, then a code variable is bound to
this list. How would one access that thing? Does replicate()[*-1]
return the last Whatever and replicate()[0] the first? How does one
make use of such a Whatever value?
> But then it's not clear how the eventual victim would know how to
> distinguish *+42 from *-42 if it's only getting some kind of closure
> to call.
I don't get that. Do you mean replicate()[*+42] that would be one
after the last Whatever and replicate()[*-42] that would be the first
Whatever in the list but indexed from the back? Or do you want to
index into the Whatevers like it would be the case for a range 0..*
which dispatches to &infix:<..>:(Int,Whatever) and returns a range
that is lazy at the end. IOW, I don't understand what
&infix:<xx>:(Whatever,Int) returns. I mean what type of Whatever
value is it? I understand that it is 42 times the same thing in
a list but what it is is unspecified for the moment. Such a Whatever
value is sort of parametric and adapts to the environment it is
finally used in. The two uses I see right now is in ranges and as
array index.
BTW, are Whatever ranges applicable to arrays?
my @a = 0,1,2,3,4,5,6,7,8,9;
my $i = 5..*;
say @a[$i]; # prints 5 6 7 8 9
Is the Whateverness of the range kept and finally resolved
when applied to the array? Or does &postcircumfix:<[ ]> see
only a lazy range?
> So I think we still have to dispatch to Whatever and let
> the Whatever code extract any relevant relative information knowing
> that a "dwim" was explicitly called for by use of *.
Could you make an example of what you mean here, please. Do you mean
a dispatch to &postcircumfix:<[ ]>?
Regards, TSa.
--
I wrote:
> my @a = 0,1,2,3,4,5,6,7,8,9;
> my $i = 5..*;
> say @a[$i]; # prints 5 6 7 8 9
And how about
my $i = 5..*-1;
say @a[$i]; # prints 5 6 7 8?
The rational would be that the -1 goes
into the range Whatever value, that is
when it comes to expanding it inside the
array index the following information is
available:
1) the start of the range = 5
2) the -1 of the Whatever
3) the array's last index = 9
these result in the following array access
say @a[5..9-1];
Instead of the array's last index the array length = 10
might be used and we end up with an access of @a[5..10-1].
But then how would $i = 5..* work? It would need to be
written $i = 5..^* to exclude the index 10.
Regards, TSa.
--