Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl6 Contest #2: P6 That Doesn't Look Like P5

by iblech (Friar)
on Jun 03, 2005 at 19:18 UTC ( [id://463374]=note: print w/replies, xml ) Need Help??


in reply to Perl6 Contest #2: P6 That Doesn't Look Like P5

What you're searching for is the outer product. Following code should do it, and works in current Pugs :)

use v6; sub outer(*@vals) { my &helper = -> @prev, @rest { if @rest { # We've still got a rest to interate over. # We add all items of @rest[0] to the new @prev # in our sub-helper, and use @rest[1...] as new # rest, i.e. all elements of @rest except the # first one. helper [ *@prev, $_ ], @rest[1...] for @rest[0]; } else { # We don't have to recurse further, so we # simply "return" @prev. take @prev; } }; # @prev: Empty array # @rest: @vals gather { helper [], @vals }; } my @a = outer [1,2,3],[4,5,6],[7,8,9]; say join "\n", @a; # 1 4 7 # 1 4 8 # [...] # 3 6 8 # 3 6 9

--Ingo

Replies are listed 'Best First'.
Re^2: Perl6 Contest #2: P6 That Doesn't Look Like P5
by dpuu (Chaplain) on Jun 03, 2005 at 21:45 UTC
    my &helper = -> @prev, @rest { ... }
    Too much syntax! You can use lexically scoped subs in P6 -- you don't have to use pointy-blocks for everything:
    my sub helper (@prev, @rest) { ... }
    Now I'm wondering if its possible to get rid of the "if" statment using multi-subs; And after that if its possible to get rid of the indexing into @rest. Something like
    sub outer(*@vals) { my multi sub helper (@prev) { take @prev } my multi sub helper (@prev, $current, *@rest) { $current.map: { helper [@prev, $_], *@rest } } gather { helper [], *@vals } }
    This is totally untested -- I haven't been able to install pugs yet.

    --Dave.

Re^2: Perl6 Contest #2: P6 That Doesn't Look Like P5
by Limbic~Region (Chancellor) on Jun 03, 2005 at 19:50 UTC
    iblech,
    Call it what you want, what I am trying to replicate is NestedLoops from Algorithm::Loops. That is, the ability to loop over an arbitrary number of lists iteratively with a relatively small memory footprint. Your solution certainly produces the same output but not 1 at a time and has everything in memory at once. That aside, it certainly helps me learn more Perl6 and exposes others as well.

    Cheers - L~R

      That is, the ability to loop over an arbitrary number of lists iteratively with a relatively small memory footprint.

      Sure, my outer does that, unless I've misunderstood something. It does not return a Code, though, but a lazy list, and it hasn't the same API as NestedLoops, as I was concentrating on the algorithm.

      Your solution certainly produces the same output but not 1 at a time and appears to hold everything in memory at once.

      Yep, this is as to get even only one item, the helper sub has to be executed recursively several times. I.e., in general:

      { ...; recurse ...; take ...; ...; } # not so good { ...; take ...; recurse ...; ...; } # better (items are yielded ASAP)

      Therefore my solution should probably not be included in a library, as the other solutions presented scale much better. I do think that my solution is rather elegant though (no for, few lines of code).

      --Ingo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://463374]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-26 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found