Iterator-Flex

 view release on metacpan or  search on metacpan

lib/Iterator/Flex/Chunk.pm  view on Meta::CPAN

package Iterator::Flex::Chunk;

# ABSTRACT: Chunk Iterator Class

use v5.28;
use strict;
use warnings;
use experimental 'signatures';

our $VERSION = '0.34';

use Iterator::Flex::Factory 'to_iterator';
use Iterator::Flex::Utils qw[ THROW STATE EXHAUSTION :IterAttrs :IterStates throw_failure ];
use Ref::Util;
use parent 'Iterator::Flex::Base';

use namespace::clean;




































sub new ( $class, $iterable, $pars = {} ) {

    my %pars     = $pars->%*;
    my $capacity = delete $pars{capacity} // 1;
    Scalar::Util::looks_like_number( $capacity ) && int( $capacity ) == $capacity && $capacity > 0
      or throw_failure( parameter => "parameter 'capacity' ($capacity) is not a positive integer" );

    $class->SUPER::new( {
            capacity => $capacity,
            src      => $iterable,
        },
        \%pars,
    );
}

sub construct ( $class, $state ) {

    throw_failure( parameter => q{'state' parameter must be a HASH reference} )
      unless Ref::Util::is_hashref( $state );

    my ( $src, $capacity ) = @{$state}{qw[ src capacity ]};

    $src
      = to_iterator( $src, { ( +EXHAUSTION ) => THROW } );

    my $self;
    my $iterator_state;

    # This iterator may have to delay signalling exhaustion for one
    # cycle if the input iterator is exhausted and the current chunk
    # is not empty
    my $next_is_exhausted = !!0;

    return {
        ( +_NAME ) => 'ichunk',

        ( +_SELF ) => \$self,

        ( +STATE ) => \$iterator_state,

        ( +NEXT ) => sub {
            return $self->signal_exhaustion
              if $iterator_state == IterState_EXHAUSTED || $next_is_exhausted;

            my @chunked;
            eval {
                push @chunked, $src->() while @chunked < $capacity;
                1;
            } or do {
                die $@
                  unless Ref::Util::is_blessed_ref( $@ )
                  && $@->isa( 'Iterator::Flex::Failure::Exhausted' );

                return $self->signal_exhaustion if !@chunked;
                $next_is_exhausted = !!1;
            };
            return \@chunked;
        },
        ( +RESET ) => sub {
            $next_is_exhausted = !!0;
        },
        ( +_DEPENDS ) => $src,
    };
}

__PACKAGE__->_add_roles( qw[
      State::Closure
      Next::ClosedSelf
      Reset::Closure
] );

1;

#
# This file is part of Iterator-Flex
#
# This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
#
# This is free software, licensed under:
#
#   The GNU General Public License, Version 3, June 2007
#

__END__

=pod

=for :stopwords Diab Jerius Smithsonian Astrophysical Observatory

=head1 NAME

Iterator::Flex::Chunk - Chunk Iterator Class

=head1 VERSION

version 0.34

=head1 METHODS

=head2 new

  $iterator = Iterator::Flex::Chunk->new( $iterable, ?\%pars );

Returns an iterator which, for each iteration, reads up to a specified
number of elements from C<$iterable>, and returns an arrayref containing
those elements.

C<$iterable> is converted into an iterator via
L<Iterator::Flex::Factory/to_iterator> if required.

The optional C<%pars> hash may contain standard L<signal



( run in 0.657 second using v1.01-cache-2.11-cpan-d8267643d1d )