Ancient
view release on metacpan or search on metacpan
t/1080-util-map-grep-for.t view on Meta::CPAN
# String functions in map/grep
# ============================================
subtest 'trim in map' => sub {
my @strings = (' hello ', 'world ', ' foo', 'bar');
my @trimmed = map { trim($_) } @strings;
is_deeply(\@trimmed, ['hello', 'world', 'foo', 'bar'], 'trim in map');
};
subtest 'ltrim/rtrim in map' => sub {
my @strings = (' hello ', ' world');
my @left = map { ltrim($_) } @strings;
my @right = map { rtrim($_) } @strings;
is_deeply(\@left, ['hello ', 'world'], 'ltrim in map');
is_deeply(\@right, [' hello', ' world'], 'rtrim in map');
};
subtest 'starts_with in grep' => sub {
my @words = qw(apple apricot banana avocado orange);
my @a_words = grep { starts_with($_, 'a') } @words;
is_deeply(\@a_words, ['apple', 'apricot', 'avocado'], 'starts_with in grep');
};
subtest 'ends_with in grep' => sub {
my @files = qw(test.txt data.csv log.txt readme.md notes.txt);
my @txt_files = grep { ends_with($_, '.txt') } @files;
is_deeply(\@txt_files, ['test.txt', 'log.txt', 'notes.txt'], 'ends_with in grep');
};
subtest 'is_empty in grep' => sub {
my @strings = ('hello', '', ' ', 'world', undef);
my @non_empty = grep { !is_empty($_) } @strings;
is(scalar(@non_empty), 3, 'is_empty filters correctly');
};
# ============================================
# Numeric functions in map
# ============================================
subtest 'clamp in map' => sub {
my @values = (-5, 0, 5, 10, 15, 20);
my @clamped = map { clamp($_, 0, 10) } @values;
is_deeply(\@clamped, [0, 0, 5, 10, 10, 10], 'clamp in map');
};
subtest 'sign in map' => sub {
my @values = (-5, -1, 0, 1, 5);
my @signs = map { sign($_) } @values;
is_deeply(\@signs, [-1, -1, 0, 1, 1], 'sign in map');
};
subtest 'min2/max2 in map' => sub {
my @pairs = ([1, 5], [3, 2], [7, 7], [0, 10]);
my @mins = map { min2($_->[0], $_->[1]) } @pairs;
my @maxs = map { max2($_->[0], $_->[1]) } @pairs;
is_deeply(\@mins, [1, 2, 7, 0], 'min2 in map');
is_deeply(\@maxs, [5, 3, 7, 10], 'max2 in map');
};
# ============================================
# Collection functions with callbacks in map/grep
# ============================================
subtest 'first in map over arrays' => sub {
my @arrays = ([1, 2, 3], [10, 20, 30], [5, 15, 25]);
# first takes a list, not arrayref - use @$_ to flatten
my @firsts = map { first(sub { $_ > 5 }, @$_) } @arrays;
is_deeply(\@firsts, [undef, 10, 15], 'first with callback in map');
};
subtest 'any in map over arrays' => sub {
my @arrays = ([1, 2, 3], [1, 10, 3], [7, 8, 9]);
my @has_gt5 = map { any(sub { $_ > 5 }, @$_) ? 1 : 0 } @arrays;
is_deeply(\@has_gt5, [0, 1, 1], 'any with callback in map');
};
subtest 'all in map over arrays' => sub {
my @arrays = ([1, 2, 3], [6, 7, 8], [10, 11, 12]);
my @all_gt5 = map { all(sub { $_ > 5 }, @$_) ? 1 : 0 } @arrays;
is_deeply(\@all_gt5, [0, 1, 1], 'all with callback in map');
};
subtest 'none in map over arrays' => sub {
my @arrays = ([1, 2, 3], [1, 6, 3], [4, 5, 6]);
my @none_gt5 = map { none(sub { $_ > 5 }, @$_) ? 1 : 0 } @arrays;
is_deeply(\@none_gt5, [1, 0, 0], 'none with callback in map');
};
subtest 'count in map over arrays' => sub {
# count matching elements using grep
my @arrays = ([1, 2, 3, 4, 5], [10, 20, 3, 4, 50], [1, 1, 1, 1, 1]);
# [1,2,3,4,5] > 3: 4, 5 = 2
# [10,20,3,4,50] > 3: 10, 20, 4, 50 = 4
# [1,1,1,1,1] > 3: none = 0
my @counts = map {
my $arr = $_;
my $c = 0;
$c++ for grep { $_ > 3 } @$arr;
$c
} @arrays;
is_deeply(\@counts, [2, 4, 0], 'count with grep in map');
};
# ============================================
# Transform functions in map
# ============================================
subtest 'pick in map over hashes' => sub {
my @hashes = (
{ a => 1, b => 2, c => 3 },
{ a => 10, b => 20, c => 30 },
);
# pick returns hashref in scalar context
my @picked = map { scalar pick($_, qw(a c)) } @hashes;
is_deeply($picked[0], { a => 1, c => 3 }, 'pick in map [0]');
is_deeply($picked[1], { a => 10, c => 30 }, 'pick in map [1]');
};
subtest 'omit in map over hashes' => sub {
my @hashes = (
{ a => 1, b => 2, c => 3 },
( run in 1.846 second using v1.01-cache-2.11-cpan-64ef6c95b5d )