Last active
February 16, 2024 11:20
-
-
Save samebchase/020df26dbe7242966b67668201863ab5 to your computer and use it in GitHub Desktop.
1brc attempt in Raku
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
#!/usr/bin/env raku | |
sub generate-stats($path) { | |
my $c = Channel.new; | |
$c.send($_) for $path.IO.lines() | |
.race(batch => 1024) | |
.map({ my @v = .split(";"); | |
@v; }); | |
$c.close; | |
my %h; | |
my $i = 0; | |
for $c.list -> ($country, $measurement) { | |
if %h{$country}:exists { | |
my ($min, $avg, $max, $count) = %h{$country}; | |
if $measurement < $min { | |
$min = $measurement; | |
} | |
if $measurement > $max { | |
$max = $measurement; | |
} | |
$count++; | |
# Incrementally calculating new average. | |
# https://blog.demofox.org/2016/08/23/incremental-averaging/ | |
$avg += ($measurement - $avg) / $count; | |
%h{$country} = [$min, $avg, $max, $count] | |
} else { | |
%h{$country} = [$measurement, $measurement, $measurement, 1] | |
} | |
$i++; | |
if $i %% 10_000 { | |
say "Processed $i rows." | |
} | |
} | |
say %h; | |
} | |
sub MAIN($file) { | |
my $path = IO::Path.new($file); | |
generate-stats($path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment