NAME

Array::Iter - Generate a coderef iterator for an array

VERSION

This document describes version 0.021 of Array::Iter (from Perl distribution Array-Iter), released on 2021-07-25.

SYNOPSIS

use Array::Iter qw(array_iter list_iter);

my $iter = array_iter([1,2,3,4,5]);
while (my $val = $iter->()) { ... }

$iter = list_iter(1,2,3,4,5);
while (my $val = $iter->()) { ... }

DESCRIPTION

This module provides a simple iterator which is a coderef that you can call repeatedly to get elements of a list/array. When the elements are exhausted, the coderef will return undef. No class/object involved.

The principle is very simple and you can do it yourself with:

my $iter = do {
    my $i = 0;
    sub {
        if ($i < @$ary) {
            return $ary->[$i++];
        } else {
            return undef;
        }
    };
 }

Caveat: if list/array contains an undef element, it cannot be distinguished with an exhausted iterator.

FUNCTIONS

array_iter($aryref) => coderef

list_iter(@elems) => coderef

HOMEPAGE

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

SOURCE

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

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Array-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.

SEE ALSO

Array::Iterator, which creates (several kinds of) iterator objects. The module also lists some other related modules.

Other *::Iter modules to create simple (coderef) iterator: Range::Iter, IntRange::Iter, NumSeq::Iter.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2021, 2015 by 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.