Hmmm... It seems that this way does lie madness -- there's a fundamental
ambiguity between autothreading happening inside or outside the declared
loop, and there's no "least surprising" way to implement it. Certainly
inside the loop is the easiest and most natural to implement, but that acts
strange from an API standpoint. Outside the loop works well from an API
standpoint but introduces all sorts of weird cases that the implementation
has to consider.
P5/PDL has some junction-like aspects, in that it does autothreading.
We sidestep the issue by not allowing multiple-element PDLs in boolean
comparisons, but treating single-element PDLs as scalars whenever possible.
This flows down to '..', so (1..pdl(3)) returns (1,2,3) or does the Right
Thing in scalar context -- but (1..pdl(3,4)) probably ought to throw an
exception (it actually returns the empty set or always-false, depending on
context).
I'm no language designer, but it seems to me that certain operators simply
won't generalize well to the autothreaded case, and those operators should
simply try to pick up the pieces -- ie treat single-element junctions like
ordinary scalars, and treat empty junctions like undef values, but otherwise
do something exceptional (whether or not that involves throwing an actual
exception). My personal preference is that constructs like 1..($a|$b) should
be disallowed. It's certainly easy enough to expand the junction manually
into a list and loop over that.
Note that other weird constructs in perl5, such as (1..[2,3]), do very
surprising, architecture-dependent things.
Quoth Ashley Winters on Friday 18 February 2005 04:09 pm,
> On Fri, 18 Feb 2005 14:35:53 -0800, Ashley Winters
>
> <[EMAIL PROTECTED]> wrote:
> > 1 .. sqrt(10) -> LazyList of (1..3)
> > 1 .. sqrt(10|20) -> Junction of any(1,2,3, 1,2,3,4)
> >
> > LazyList does Iterator, but Junction does not. You'd have to use (1 ..
> > sqrt(3|6)).values to iterate through the possible values
> > semi-randomly.
>
> Okay, changed my mind again.
>
> 1 .. sqrt(10|20) -> Junction of any(1,2,3, 1,2,3,4)
>
> therefore, for(1 .. sqrt(10|20)) iterates ONCE, but $_ is a junction.
> Anything inside the loop which uses $_ would autothread. Anything
> which doesn't use $_ would only get called once. That's insane, right?
>
> for(1 .. sqrt(10|20) {
> if($_ > 2) {} # uses junctive value
> say "Here"; # called once
> bar($_); # calls bar() lots of times
> }
>
> > More likely, I'm nuts.
>
> I'm definitely crazy. I give up! I'll stop now, since I clearly don't get
> it. :)
>
> Ashley Winters