in reply to What should be an easy math question using %
So we start out with the initial value of the first argument and then increment by the second argument or 5 by default.sub seq { my($v, $i) = ($_[0], $_[1] || 5); my $n = 0; return sub { $n ? ($n += $i) : ($n = $v) }; } my $iter = seq 8 % 3, 5; print join ', ' => map $iter->(), 1 .. 5; print " ...\n"; $iter = seq 8 % 5; print join ', ' => map $iter->(), 1 .. 5; print " ...\n"; __output__ 2, 7, 12, 17, 22 ... 3, 8, 13, 18, 23 ...
_________
broquaint
update: dropped block as first argument as it was rather unnecessary
|
|---|