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
|
#!/usr/bin/env perl
use 5.036;
use utf8;
use open ':std', ':encoding(UTF-8)';
use Sq;
use Sq::Sig;
# functional-style
my $countup = assign {
my $bill1 = Seq->range(1,1_000_000_000);
my $is_even = sub($x) { $x & 1 };
Seq::flatten_array(
Seq::zip(
Seq::filter($bill1, $is_even),
Seq::remove($bill1, $is_even),
)
);
};
run(sub {
$countup->iter(sub($x){
if ( $x % 10_000 == 0 ) {
print "First: $x\n";
}
});
});
# procedural / oo-style
my $bill1 = Seq->range(1,1_000_000_000);
my $is_even = sub($x) { $x & 1 };
my $evens = $bill1->filter($is_even);
my $unevens = $bill1->remove($is_even);
my $zip = $evens->zip($unevens);
my $flatten = $zip->flatten_array;
run(sub {
$flatten->iter(sub($x){
if ( $x % 10_000 == 0 ) {
print "Second: $x\n";
}
});
});
sub run($f) {
my $start = time();
$f->();
my $stop = time();
printf "Time: %d seconds\n", $stop;
}
|