Skip to content

Instantly share code, notes, and snippets.

@samebchase
Last active February 16, 2024 11:20
Show Gist options
  • Save samebchase/020df26dbe7242966b67668201863ab5 to your computer and use it in GitHub Desktop.
Save samebchase/020df26dbe7242966b67668201863ab5 to your computer and use it in GitHub Desktop.
1brc attempt in Raku
#!/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