NAME
Ekorn::Pipeline::Stages::Batch - stages gathering values: batch and collect
SYNOPSIS
use Ekorn::Pipeline;
use Ekorn::Pipeline::Stages::Batch; # batch and collect
# Values 500 at a time
my $batches = Ekorn::Pipeline->new(batch(500))->iterator($source);
while (my($batch) = $batches->()) { ... }
# Every value, as one result
my($all) = Ekorn::Pipeline->new(collect)->(@values);
# One value made from every value
my($names) = Ekorn::Pipeline->new(
sub { $_->{name} },
collect { join ', ', @_ },
)->(@users);
DESCRIPTION
Stages that gather values and pass them on together. Both are made with Ekorn::Pipeline::Gather, so what they hold starts afresh on each run. Both pass on what they still hold once the values run out. Each function returns a stage, and checks its arguments when it's called, not on each value.
Each stage is an object, an Ekorn::Pipeline::Stage or, for those that keep state, an Ekorn::Pipeline::Gather, called as a code reference, so it goes wherever a stage goes. Code testing it with ref or is_coderef sees an object: v0.2.0 gave code references.
FUNCTIONS
Both are exported by default, and by :all.
batch
batch($size)
Passes values on in batches of $size, each an array reference. Once the values run out, the last batch goes on, which may be shorter. There's never an empty batch: with no values, there's no batch at all. A batch is made from many values, so has no original, from v0.5.0; before, one filled by a value had that value's ("kept_origin" in Ekorn::Pipeline::Context).
collect
collect
collect { ... }
Holds every value. Once they run out, it passes on an array reference of them. Given a block, it calls the block once, with the values as its arguments, and passes on what the block returns: nothing, one value, or several, just like a stage. The block is called even with no values, so collect always gives a result: an empty array, or what the block makes of none.
collect holds every value until the end, so the whole result is in memory at once. batch holds no more than one batch.
DIAGNOSTICS
- Expected a batch size of 1 or more
-
batchwas given something other than a whole number above 0. - Expected a code reference
-
collectwas given something other than a block or code reference.
SEE ALSO
Ekorn::Pipeline, Ekorn::Pipeline::Gather
AUTHOR
Iain Campbell <cpanic@cpan.org>
LICENSE
Copyright (c) 2024-2026 by Iain Campbell.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.