On Thu, Dec 25, 2008 at 01:38:54PM -0500, Mark J. Reed wrote:
> Yeah, I tried a couple zip-based variants, but thought the flattening
> was a little confusing when combined with the reversal (so keys and
> values get swapped when the list is reversed).   I'd forgotten about
> the ^max shorthand, though.  Thanks for the reminder.
> 
> So how to loop over a list of sublists one at a time, passing each
> item to a sub of arity > 1, automatically expanding each item into
> separate parameters? Ruby does it automatically based on arity, but
> Ruby doesn't support loop-by-n-at-a-time, so there's no conflict...


There's always:

    for @gifts[$^day].pairs.reverse { my ($n,$g) = .kv; ... }

Pm


> On 12/25/08, Patrick R. Michaud <pmich...@pobox.com> wrote:
> > On Thu, Dec 25, 2008 at 12:39:24PM -0500, Mark J. Reed wrote:
> >> On Thu, Dec 25, 2008 at 12:00 PM, Patrick R. Michaud <pmich...@pobox.com>
> >> wrote:
> >> > On Thu, Dec 25, 2008 at 12:53:06AM -0500, Mark J. Reed wrote:
> >> >> I also tried this, but it caused Rakudo to throw a StopIteration and
> >> >> then segfault:
> >> >>
> >> >> for [...@gifts[0..$day-1]].pairs.reverse -> $n, $g
> >> >
> >> > The StopIteration occurs when there aren't enough elements in the
> >> > list to supply to the parameters to the body.  In the example above,
> >> > it would occur whenever there are an odd number of pairs.
> >>
> >> OK, so that loops through the list in groups of two.  So how do I Ioop
> >> through a list of Pairs assigning the key to one var and the value to
> >> another?  It's not a Hash, and calling .kv on it yields 0=>first pair,
> >> 1=>second pair, etc... I don't want to Hashify it because I want to
> >> preserve the order...
> >
> > Extracting the keys+values from a list of Pairs was a question that
> > occurred to me as well when implementing it... I don't know the answer
> > to that.
> >
> > However, for this specific problem, perhaps the zip operator is a
> > better choice anyway:
> >
> >     for (^$day Z @gifts).reverse -> $g, $n { ... }
> >
> > I'm not entirely sure of the order of the $g, $n arguments here,
> > or exactly how .reverse chooses to change the order of the items
> > produced by infix:<Z> (does it flatten or no?).  But some permutation
> > of this should work.
> >
> > Pm
> >
> 
> -- 
> Sent from Gmail for mobile | mobile.google.com
> 
> Mark J. Reed <markjr...@gmail.com>

Reply via email to