-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
List::MoreUtils
executable file
·83 lines (68 loc) · 1.46 KB
/
List::MoreUtils
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl
# All functions from the List::MoreUtils module ported using map
use strict;
use List::Util 'shuffle';
$\ = "\n";
my @foo = map{ sprintf("%03d", $_) } (0..200);
{
# List::MoreUtils::all
my $i = 0;
map { /\d+/ and $i++; } @foo;
print 'all: yes' if($i == scalar(@foo));
}
# List::MoreUtils::none
print 'none: no' if ! map {my $i = 0; /\d{4}/ and $i++; $i } @foo;
# List::MoreUtils::any
print 'any: yes' if map { my $i = 0; /^19\d/ and $i++; $i } @foo;
{
# List::MoreUtils::true
my $i = 0;
print "true: $i" if map { $_ and $i++; } @foo;
}
{
# List::MoreUtils::false
push(@foo, (0) x sprintf("%.d", rand(128)));
my $i = 0;
print "false: $i" if map { !$_ and $i++; } @foo;
}
{
# List::MoreUtils::lastidx
sub lastidx {
my %map;
my $i;
for(shuffle(@_)) {
if(/^1(?:3|9)/) {
$map{$i} = $_;
}
$i++;
}
for(sort { $a <=> $b } (keys(%map))) {
return($_);
}
}
print "lastidx: " . lastidx(@foo);
}
{
# List::MoreUtils::firstidx
sub firstidx {
my %map;
my $i;
for(shuffle(@_)) {
if(/^1(?:3|9)/) {
$map{$i} = $_;
}
$i++;
}
for(sort { $map{$a} <=> $map{$b} } (keys(%map))) {
return($_);
}
}
print "firstidx: " . firstidx(@foo);
}
{
# List::MoreUtils::insert_after
my $i = 0;
my $n = sprintf("%.d", rand(9));
map{ $_ =~ /$n/ and $_ .= ' foobar'; } @foo;
print "insert_after: $i" if map { / foobar$/ and $i++; $i; } @foo;
}