Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

anon-sub()'s execute in a for??

7 views
Skip to first unread message

Paul

unread,
Apr 22, 2003, 1:18:01 PM4/22/03
to perl6-l...@perl.org

Directly from S6 under "Pointy subs":
=====================================
: for @list -> $elem { # Same as: for @list, sub ($elem) {
: print "$elem\n"; # print "$elem\n";
: } # }

Ok, no problem....
But that means that the for loop is going to execute the anonymous sub?
THAT's a bit different, isn't it?

Hmm.... time for a little code experiment, to polish the learning
nerves.

That means for is defined *something* like this?

# quick and clumsy first draft :)
sub for ( @_ is rw, &loopcode) {
my $ndx = 0;
while $ndx < @_ {
&loopcode(@_[$ndx++]);
}
}

I know that's not the postfix version, which is probably an infix multi
operator anyway. I'm still fuzzy on CALLER and OUTER and how they play
with $_, so I didn't want to get into the invocant.... but I expect it
would be possible to define it that way. Likewise, I didn't *EVEN* want
to try to code for multiple topicalizers! But does that make sense at
the simplistec first-glance level?

Again, Hmm....

__________________________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo
http://search.yahoo.com

Paul

unread,
Apr 22, 2003, 1:22:17 PM4/22/03
to perl6-l...@perl.org

--- Paul <ydb...@yahoo.com> wrote:
> simplistec

heh -- "I meant to do that...."

Luke Palmer

unread,
Apr 22, 2003, 2:30:28 PM4/22/03
to Hod...@writeme.com, perl6-l...@perl.org
> Directly from S6 under "Pointy subs":
> =====================================
> : for @list -> $elem { # Same as: for @list, sub ($elem) {
> : print "$elem\n"; # print "$elem\n";
> : } # }
>
> Ok, no problem....
> But that means that the for loop is going to execute the anonymous sub?
> THAT's a bit different, isn't it?
>
> Hmm.... time for a little code experiment, to polish the learning
> nerves.
>
> That means for is defined *something* like this?
>
> # quick and clumsy first draft :)
> sub for ( @_ is rw, &loopcode) {
> my $ndx = 0;
> while $ndx < @_ {
> &loopcode(@_[$ndx++]);
> }
> }
>
> I know that's not the postfix version, which is probably an infix multi
> operator anyway. I'm still fuzzy on CALLER and OUTER and how they play
> with $_, so I didn't want to get into the invocant.... but I expect it
> would be possible to define it that way. Likewise, I didn't *EVEN* want
> to try to code for multiple topicalizers! But does that make sense at
> the simplistec first-glance level?

Yep. Almost perfect syntax, too (that & shouldn't be there on line
5). Multiple topicalizers (you probably meant multiple
parameters... multiple topicalizers makes no sense) isn't that hard,
especially given the C<zip> operator which does the hard stuff for us.

sub for ( @items, &code ) {
my $i = 0;
while $i < @items {
code( *@items[$i..$i + &code.req - 1] ); # [0]
$i += &code.req;
}
}

There needs to be some stuff for handling FIRST, NEXT, etc. blocks,
but that likely just entails a &code.first(), &code.next(), etc. in
the appropriate places.

I didn't bother with the rw stuff, because the rules for that are
complicated. As a question for anyone who knows, how do I say that I
want @items to be copied, but that I want its elements to still be
bound (so I could, say, splice on @items and not have it reflect back,
but @items[1]++ would). And perhaps some other combinations (a const
array of rw elements, an entirely const array, ...)?

Luke

[1] The - 1 above illustrates what's always bugged me about slices.
Is there a chance to have the range operator do the range [$a, $b)
instead, which is more friendly mathematically? (That would make the
mathematicians happy with 1..Inf, too :)

Paul

unread,
Apr 22, 2003, 3:50:04 PM4/22/03
to Luke Palmer, perl6-l...@perl.org

--- Luke Palmer <fibo...@babylonia.flatirons.org> wrote:
> > Directly from S6 under "Pointy subs":
> > =====================================
> > : for @list -> $elem { # Same as: for @list, sub ($elem) {
> > : print "$elem\n"; # print "$elem\n";
> > : } # }
> >
> > Ok, no problem....
> > But that means that the for loop is going to execute the anonymous
> > sub? THAT's a bit different, isn't it?
> >
> > Hmm.... time for a little code experiment, to polish the learning
> > nerves.
> >
> > That means for is defined *something* like this?
> >
> > # quick and clumsy first draft :)
> > sub for ( @_ is rw, &loopcode) {
> > my $ndx = 0;
> > while $ndx < @_ {
> > &loopcode(@_[$ndx++]);
> > }
> > }
> >
> > I know that's not the postfix version, which is probably an
> > infix multi operator anyway. I'm still fuzzy on CALLER and
> > OUTER and how they play with $_, so I didn't want to get into
> > the invocant.... but I expect it would be possible to define
> > it that way. Likewise, I didn't *EVEN* want to try to code for
> > multiple topicalizers! But does that make sense at the simplistic

> > first-glance level?
>
> Yep. Almost perfect syntax, too (that & shouldn't be there on
> line 5).

Ha! Be careful, you'll swell my head.....
And I started to take it off, then started to look it up....
Then decided to wing it and see how close I could get. :)

> Multiple topicalizers (you probably meant multiple
> parameters... multiple topicalizers makes no sense)

Er? Maybe I said it wrong, but just to see if I'm on track (and I'm not
looking up the syntax for this, either):

for @a;@b -> $a;$b { print "$a $b\n" }

I think the "izer" shouldn't've been. Just "multiple topicals"?

I guess those are the params, though, aren't they? but what's the
syntactic equiv? I mean " -> $a { print $a } " approximates " sub ($a)
{ print $a } ", so what approximates " -> $a;$b { print "$a $b\n" } "?

My point being that

for @a -> $a { print $a }

is a *bit* like saying

sub foo ($a) { print $a }
foo($_) for @a;

and

for @a;@b -> $a;$b { print "$a $b\n" }

is vaguely like

sub foo ($a, $b) { print "$a $b\n" }
foo($a,$b) while ($a,$b) = splice @c, 0, 2;

except that I'm using named subs here. In other words, I'm having
another density attack, but the question is, what's the sub()
equivelent? Multiple topicalized variables are something that magically
seems to happen in the context, and I suspect that they are a result of
the way the parser reads the code, but to learn the language a little
better....how would you write that for{} construct using the other
existing primitives of the language?

Geez, am I making any sense here at all? lol!

> isn't that hard, especially given the C<zip> operator which
> does the hard stuff for us.

"zip"??? You mean the flattening asterisk? You lost me.

> sub for ( @items, &code ) {
> my $i = 0;
> while $i < @items {
> code( *@items[$i..$i + &code.req - 1] ); # [0]
> $i += &code.req;
> }
> }

&code.req ??

And why not just pass in each element sequentially?

> There needs to be some stuff for handling FIRST, NEXT, etc. blocks,
> but that likely just entails a &code.first(), &code.next(), etc. in
> the appropriate places.

ok, presuming someone added those blocks, I could see you'd need to do
that.... or am I missing something?



> I didn't bother with the rw stuff, because the rules for that are
> complicated. As a question for anyone who knows, how do I say that I
> want @items to be copied, but that I want its elements to still be
> bound (so I could, say, splice on @items and not have it reflect
> back, but @items[1]++ would). And perhaps some other combinations
> (a const array of rw elements, an entirely const array, ...)?

hm, good questions.

Luke Palmer

unread,
Apr 22, 2003, 4:34:23 PM4/22/03
to Hod...@writeme.com, perl6-l...@perl.org
Paul wrote:
> > Multiple topicalizers (you probably meant multiple
> > parameters... multiple topicalizers makes no sense)
>
> Er? Maybe I said it wrong, but just to see if I'm on track (and I'm not
> looking up the syntax for this, either):
>
> for @a;@b -> $a;$b { print "$a $b\n" }
>
> I think the "izer" shouldn't've been. Just "multiple topicals"?

IIRC, that syntax isn't around anymore. Or if it is, it's a syntactic
sugar hack. The canonical form at the moment is:

for zip(@a, @b) -> $a, $b { print "$a $b\n" }

C<zip> is a lazy operator that interleaves lists (and does a bunch of
other things that I can't remember at the moment). The simple form
might look like this:

sub zip_coroutine(@arrays of Array) {
for 0..max(*@arrays>>.<<length)-1 -> $i {
for @arrays -> $a {
yield $a.[$i]
}
}
}
sub zip(Array *@arrays) {
@( <zip_coroutine(@arrays)> )
}

I think... I used a lot of features that I'm not very familiar with
there.

> I guess those are the params, though, aren't they? but what's the
> syntactic equiv? I mean " -> $a { print $a } " approximates " sub ($a)
> { print $a } ", so what approximates " -> $a;$b { print "$a $b\n" } "?

It would be approximately C<sub ($a,$b) { print "$a $b\n" }>, and the
sugar would go on the C<for> side.

> > isn't that hard, especially given the C<zip> operator which
> > does the hard stuff for us.
>
> "zip"??? You mean the flattening asterisk? You lost me.

No, the thing I displayed above. And it desperately needs renaming. :)

> > sub for ( @items, &code ) {
> > my $i = 0;
> > while $i < @items {
> > code( *@items[$i..$i + &code.req - 1] ); # [0]
> > $i += &code.req;
> > }
> > }
>
> &code.req ??

Yeah, from A6, the number of required arguments to a function---the
C<arity> (as it might be called now... don't remember).

> And why not just pass in each element sequentially?

Because you asked for more than one at a time.

Luke

Paul

unread,
Apr 22, 2003, 4:45:35 PM4/22/03
to Luke Palmer, perl6-l...@perl.org
> > > Multiple topicalizers (you probably meant multiple
> > > parameters... multiple topicalizers makes no sense)
> >
> > Er? Maybe I said it wrong, but just to see if I'm on track (and I'm
> > not looking up the syntax for this, either):
> >
> > for @a;@b -> $a;$b { print "$a $b\n" }
> >
> > I think the "izer" shouldn't've been. Just "multiple topicals"?
>
> IIRC, that syntax isn't around anymore. Or if it is, it's a
> syntactic sugar hack. The canonical form at the moment is:
>
> for zip(@a, @b) -> $a, $b { print "$a $b\n" }
>
> C<zip> is a lazy operator that interleaves lists (and does a bunch of
> other things that I can't remember at the moment).

Oh, boy.... (~sigh~)
Where's *that*? :)

> The simple form might look like this:
>
> sub zip_coroutine(@arrays of Array) {
> for 0..max(*@arrays>>.<<length)-1 -> $i {
> for @arrays -> $a {
> yield $a.[$i]
> }
> }
> }
> sub zip(Array *@arrays) {
> @( <zip_coroutine(@arrays)> )
> }
>
> I think... I used a lot of features that I'm not very familiar with
> there.

lol -- huh? Could you repeat that? I lost a lot of it in the line
noise!
^grin^

The thing that really threw me was >>.<< which I haven't seen enough to
have a clue about.



> > I guess those are the params, though, aren't they? but what's the
> > syntactic equiv? I mean " -> $a { print $a } " approximates " sub
> > ($a) { print $a } ", so what approximates " -> $a;$b { print
> > "$a $b\n" } "?
>
> It would be approximately C<sub ($a,$b) { print "$a $b\n" }>, and the
> sugar would go on the C<for> side.

And syntactic sugar tends to be parser magic. :)
That makes me happier.

> > > isn't that hard, especially given the C<zip> operator which
> > > does the hard stuff for us.
> >
> > "zip"??? You mean the flattening asterisk? You lost me.
>
> No, the thing I displayed above. And it desperately needs renaming.
> :)

lol -- yeah, though at least I get it.

> > > sub for ( @items, &code ) {
> > > my $i = 0;
> > > while $i < @items {
> > > code( *@items[$i..$i + &code.req - 1] ); # [0]
> > > $i += &code.req;
> > > }
> > > }
> >
> > &code.req ??
>
> Yeah, from A6, the number of required arguments to a function---the
> C<arity> (as it might be called now... don't remember).

I remember the discussion on the list right about when I joined.



> > And why not just pass in each element sequentially?
>
> Because you asked for more than one at a time.

Duh.

0 new messages