Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: How to return a list using recursion?

by Athanasius (Chancellor)
on Aug 07, 2012 at 04:26 UTC ( #985874=note: print w/replies, xml ) Need Help??


in reply to Re: How to return a list using recursion?
in thread How to return a list using recursion?

this isn't something you should use recursion for

s/should/need to/

You also don’t need a module. Here is a solution using a stateful subroutine as an iterator:

#! perl use strict; use warnings; my @list = ('a' .. 'z'); my $size = 5; _array_chunk_it($size, @list); while (my @chunk = _array_chunk_it()) { print "Array chunk of $size =\n", join("\n", @chunk), "\n\n"; } { my ($array, $count); sub _array_chunk_it { my @chunk; if (@_) { $count = shift; $array = [ @_ ]; } else { for (1 .. $count) { push(@chunk, shift @$array) if @$array; } } return @chunk; } }

(With newer versions of Perl, this can be rewritten using state.)

TMTOWTDI :-)

Update: ++Anonymous Monk for the superior version using a closure, below.

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^3: How to return a list using recursion?
by Anonymous Monk on Aug 07, 2012 at 15:04 UTC

    With older versions of Perl, it can be rewritten as a proper iterator :-)

    use strict; use warnings; my @list = ('a' .. 'z'); my $size = 5; my $chunker = _array_chunk_it($size, @list); while (my @chunk = $chunker->()) { print "Array chunk of $size =\n", join("\n", @chunk), "\n\n"; } sub _array_chunk_it { my ($count, @array) = @_; return sub { my @chunk; for (1 .. $count) { push(@chunk, shift @array) if @array; } return @chunk; } }

    (Using a closure we can actually call _array_chunk_it multiple times, in an overlapping manner. Have multiple iterators running simultaneously. Plus it looks quite a bit nicer.)

Re^3: How to return a list using recursion?
by Anonymous Monk on Aug 08, 2012 at 08:32 UTC

    this isn't something you should use recursion for

    s/should/need to/

    :) I meant should not, esp since there is nothing particularly recursive about it (you have to squint to make it recursive)

    You also donít need a module.

    Sure I do :) otherwise I'm writing the the idiomatic perl solution

    It was real hard to come up with

    #!/usr/bin/perl -- use strict; use warnings; my @x = ('a' .. 'z'); print yup( 5, @x ),"\n--\n"; print yup( 3, @x ),"\n"; sub yup { my( $n, @x ) = @_; my @yo; for( my $ix = 0; $ix < @x; $ix += $n ) { my $high = $n - 1 + $ix ; $high > $#x and $high = $#x ; push @yo, join ' ', @x[ $ix .. $high ]; } return join ",\n", @yo; } __END__ a b c d e, f g h i j, k l m n o, p q r s t, u v w x y, z -- a b c, d e f, g h i, j k l, m n o, p q r, s t u, v w x, y z

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985874]
help
Chatterbox?
[Corion]: marioroy: Oh, that's always cool, having API-compatible modules. This makes testing and comparing things much easier
[marioroy]: IPC in MCE::Shared can handle 400k (sends) per second. That's seems a lot for being a pure-Perl module. After making the release, will come back and post a solution for a node by a fellow wanting faster logging.
[Corion]: While working on WWW::Mechanize:: Chrome, I had the suspicion that AnyEvent was doing something wrong, but I was able to swap it out for Mojolicious and the error persisted.
[Corion]: Of course, the error was in my own code ;)
[marioroy]: Corion, start and start_child in MCE::Hobo::Manager return a MCE::Hobo object, whereas P::FM returns the PID. I can have it return the PID though. I tried Hobo::Manager with several P::FM modules, just changed P::FM to MCE::Hobo::Manager and it works.
[marioroy]: I also have a Hobo driver for Forklift allowing folks to use in multiple classes, no conflicts with one another. That's not possible for P::FM.
[Discipulus]: congrats marioroy!
[marioroy]: CORE::wait works well eventhough multiple instances or classes using Hobo::Manager.
[Corion]: marioroy: I'm not sure what the normal use for the PID is in P:FM, but I guess that most programs just ignore or log it
[Corion]: Oh, yes, programs could call wait $pid, but if your $pid is an object, then you could add a ->wait method to it and wait $pid would call that automatically "thanks" to indirect object notation

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-05-26 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?