-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement streaming helpers to for/map/grep/reduce items in an io object
- Loading branch information
1 parent
4d1cfd1
commit 9dc1955
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use IO::All -binary; | ||
|
||
my $t = -e 't' ? 't' : 'test'; | ||
my $io = io( "$t/mystuff" ); | ||
|
||
my %hash; | ||
my @for = $io->for( sub { $hash{$_} = length } ); | ||
is_deeply \%hash, { "No bluff.\n" => 10, "is quite enough.\n" => 17, "My stuff\n" => 9 }, | ||
"for runs a bit of code on each line"; | ||
is_deeply \@for, [], "for returns nothing"; | ||
|
||
my @map = $io->map( sub { "$_+", "-" } ); | ||
is_deeply \@map, [ "My stuff\n+", "-", "is quite enough.\n+", "-", "No bluff.\n+", "-" ], | ||
"map returns modified version of the input lines"; | ||
|
||
my @grep = $io->grep( sub { /uff/ } ); | ||
is_deeply \@grep, [ "My stuff\n", "No bluff.\n" ], "grep finds the lines for which \$code is true"; | ||
|
||
my $reduce = $io->reduce( sub { shift() + length }, 0 ); | ||
is $reduce, 36, "reduce returns an accumulator based on the input lines"; | ||
|
||
my $grep_dir = io->dir($t)->grep( sub { !/\./ } ); | ||
is $grep_dir, 9, "grep works on directories too"; | ||
|
||
done_testing; |