Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: How to return a list using recursion?

by Anonymous Monk
on Aug 07, 2012 at 03:39 UTC ( [id://985869]=note: print w/replies, xml ) Need Help??


in reply to How to return a list using recursion?

Well, this isn't something you should use recursion for

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use List::AllUtils qw' natatime '; my @x = ('a' .. 'z'); my $it = natatime 5, @x; my @yo; while (my @vals = $it->()) { push @yo, "@vals,\n"; } chomp $yo[-1]; chop $yo[-1]; dd \@yo; __END__ [ "a b c d e,\n", "f g h i j,\n", "k l m n o,\n", "p q r s t,\n", "u v w x y,\n", "z", ]

Replies are listed 'Best First'.
Re^2: How to return a list using recursion?
by Athanasius (Archbishop) on Aug 07, 2012 at 04:26 UTC
    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

      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.)

      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://985869]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-25 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found