The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Seq::Iter - Generate a coderef iterator from a sequence of items, the last of which can be a coderef to produce more items

VERSION

This document describes version 0.001 of Seq::Iter (from Perl distribution Seq-Iter), released on 2023-11-21.

SYNOPSIS

 use Seq::Iter qw(seq_iter);

# generate fibonacci sequence
my $iter = seq_iter(1, 1, sub { my ($index, $orig_seq, $gen_seq) = @_; $gen_seq->[0] + $gen_seq->[1] }); # => 1, 1, 2, 3, 5, 8, ...
# ditto, shorter
my $iter = seq_iter(1, 1, sub { $_[2][0] + $_[2][1] });

# generate 5 random numbers
my $iter = seq_iter(sub { my ($index, $orig_seq, $gen_seq) = @_; $index >= 5 ? undef : sprintf("%.3f", rand()) }); # => 0.238, 0.932, 0.866, 0.841, 0.501, undef, ...

# randomly decrease between 0.1 and 0.4 then always return 0 after it reaches <= 0
my $iter = seq_iter(3, sub { my ($index, $orig_seq, $gen_seq) = @_; $gen_seq->[0] <= 0 ? 0 : $gen_seq->[1]-(rand()*0.3+0.1)));

DESCRIPTION

This module provides a simple (coderef) iterator which you can call repeatedly to get numbers specified in a sequence specification (list). The last item of the list can be a coderef which will be called to produce more items. The coderef item will be called with:

($index, $orig_seq, $gen_buf)

where $index is a incrementing number starting from 0 (for the first item of the generated sequence), $orig_seq is the original sequence arrayref, and $gen_buf is an array containing generated items (most recent items first), capped at $BUFFER_SIZE items (by default 10).

VARIABLES

$BUFFER_SIZE

FUNCTIONS

seq_iter

Usage:

$iter = seq_iter(LIST); # => coderef

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Seq-Iter.

SOURCE

Source repository is at https://github.com/perlancar/perl-Seq-Iter.

SEE ALSO

For simpler number sequence, see NumSeq::Iter. As of 0.006, the module supports recognizing fibonacci sequence.

Other *::Iter modules.

AUTHOR

perlancar <perlancar@cpan.org>

CONTRIBUTING

To contribute, you can send patches by email/via RT, or send pull requests on GitHub.

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

% prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Seq-Iter

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.