NAME
Ekorn::Pipeline::Stages::Flow - stages taking or dropping values: take, drop, take_if, drop_if, take_while and drop_while
SYNOPSIS
use Ekorn::Pipeline;
use Ekorn::Pipeline::Stages::Flow; # take and drop
# The third page of ten
my @page = Ekorn::Pipeline->new(
drop(20),
take(10),
)->(@names);
use Ekorn::Pipeline::Stages::Flow qw(:DEFAULT take_while drop_while);
# A mail's headers: its lines up to the first blank one
my $headers = Ekorn::Pipeline->new(take_while { m( \S )x })
->iterator(sub { scalar(<$fh>) // () });
DESCRIPTION
Stages that decide which values go on, by their position or by a test, whatever the values are. Each function returns a stage. It 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.
take, drop, take_last, take_single and drop_while keep state, so they're made with Ekorn::Pipeline::Gather and start afresh on each run. A run is a call of a pipeline with values, or an iterator or a feed made from it.
take_if and drop_if test every value; take_while and drop_while stop testing at the first value the test fails, one by ending the pipeline there, the other by passing on everything after.
take, take_first, take_single and take_while end the pipeline with Ekorn::Pipeline::Stop once they've passed on all they will, so an iterator stops calling its source.
FUNCTIONS
take and drop are exported by default. take_first, take_last, take_single, take_if, drop_if, take_while and drop_while are exported on request, and :all gives all nine. To get the defaults and one more, name :DEFAULT too:
use Ekorn::Pipeline::Stages::Flow qw(:DEFAULT take_if);
take
take($count)
Passes on the first $count values, then stops the pipeline. If there are fewer, it passes them all on. take(0) passes on none, but it still sees the first value, because a stage isn't called until there is one.
drop
drop($count)
Drops the first $count values, and passes on the rest. With take, it gives a page:
drop(20), take(10) # the 21st to the 30th
take_first
take_first
Passes on the first value, then stops the pipeline: take(1), named as the stage of the sink keep_first.
take_last
take_last
Passes on the last value, once there are no more, reading every one to find it; nothing if there were none. If a stage before it stops the pipeline, it passes on the last value to reach it. It holds values back, and says where its value came from, so the stages after it get that value's original, from v0.5.0; before, it had none, as a flush's values have ("kept_origin" in Ekorn::Pipeline::Context).
Each take_... stage passes on what the keep_... sink of the same name, in Ekorn::Pipeline::Sinks, gives as its result, so the stages after it can go on: take($n) is keep($n)'s, and take_first, take_last and take_single are keep_first's, keep_last's and keep_single's. keep_all has none, as its stage would pass every value on, and be no stage at all. A pipeline ending in a take stage, where its sink was forgotten, gives the values in list context, and how many in scalar context, never a wrong one.
take_single
take_single
Passes on the one value. At a second, it warns "Expected only one item, but there were more" and stops the pipeline, so the first is all that goes on: for when one value was meant, and its limit forgotten.
take_if
take_if { ... }
Passes on each value the block returns true for, and drops the rest: a filter, as grep is, testing every value. The block gets each value in $_ and as its argument.
take_if { $_->{plays} >= 6000 } # every hit
A plain stage ending in a test, sub { $_-{plays} >= 6000 }>, passes on what the test returns, true or false, not the value; take_if passes on the value.
drop_if
drop_if { ... }
Drops each value the block returns true for, and passes on the rest: take_if's opposite. The block gets each value in $_ and as its argument.
take_while
take_while { ... }
Passes on values while the block returns true for them, then stops the pipeline at the first value it returns false for. That value isn't passed on. The block gets each value in $_ and as its argument. Unlike take_if, it tests no value after the first that fails, so it reads no further.
drop_while
drop_while { ... }
Drops values while the block returns true for them, then passes on the rest, starting with the first value it returns false for. After that, the block isn't called again on that run. The block gets each value in $_ and as its argument.
DIAGNOSTICS
- Expected a count of 0 or more
-
takeordropwas given something other than a whole number. - Expected a code reference
-
take_if,drop_if,take_whileordrop_whilewas given something other than a block or code reference. - Expected only one item, but there were more
-
A warning:
take_singlewas given a second value, and stopped the pipeline, passing on only the first.
SEE ALSO
Ekorn::Pipeline, Ekorn::Pipeline::Gather, Ekorn::Pipeline::Stop
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.