use Kavorka qw(fun); use Scalar::Induce qw(induce void); fun repeat($val!, $times!) { induce { $_-- ? $val : void undef $_ } $times; } repeat('foo', 5); # ('foo', 'foo', 'foo', 'foo', 'foo') repeat({bar => 42}, 3); # 0 HASH(0x138be78) # bar => 42 # 1 HASH(0x138be78) # -> REUSED_ADDRESS # 2 HASH(0x138be78) # -> REUSED_ADDRESS fun range(:$from = 1, :$to!, :$step = 1) { induce { my $curr = $_; undef $_ if ($_ += $step) > $to; return $curr; } $from; } range(step => 1.1, from => 4, to => 17); # (4, 5.1, 6.2, 7.3, 8.4, 9.5, 10.6, 11.7, 12.8, 13.9, 15, 16.1) range(to => 7); # (1, 2, 3, 4, 5, 6, 7) fun partition($n!, @l!) { induce { my @part = splice @$_, 0, $n; undef $_ unless @$_; return \@part; } \@l } partition(3, qw(Aragorn Boromir Frodo Gandalf Gimli Legolas Merry Pipp +in Sam)); # ( # ['Aragorn', 'Boromir', 'Frodo'], # ['Gandalf', 'Gimli', 'Legolas'], # ['Merry', 'Pippin', 'Sam'] # ) fun factor($n!) { induce { for my $i (2..$_/2) { unless ($_ % $i) { $_ /= $i; return $i; } } my $curr = $_; undef $_; return $curr; } $n } factor(138600); # (2, 2, 2, 3, 3, 5, 7, 11)

In reply to fun with induce by daxim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.