Skip to content

Instantly share code, notes, and snippets.

@masak
Created February 19, 2010 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/309333 to your computer and use it in GitHub Desktop.
Save masak/309333 to your computer and use it in GitHub Desktop.
sub load_data($filename, $version = 1, *@dirpath = @last_dirpath //= @std_dirpath) {
# RAKUDO: @dirpath >>~~<< s{(<-[/]>)$}{$1/};
.=subst(/(<-[/]>)$/, {"$1/"}) for @dirpath;
my %data;
for @dirpath -> $prefix {
my $filepath = $prefix . $filename;
# RAKUDO: :w and :r not implemented
if $filepath ~~ :e && 100 < $filepath ~~ :s < 1e6 {
# RAKUDO: There's no 'rw' mode in &open
my $fh = open $filepath, :r
or die "Something screwy with $filepath: $!";
my ($name, $vers, $status, $costs) = $fh.lines(4);
$costs = [$costs.comb(/\S+/)];
%data{$filepath} = {}; # RAKUDO: Autovivification
%data{$filepath}< filepath name vers stat costs> =
($filepath, $name, $vers, $status, $costs);
}
}
return %data;
}
constant @StartOfFile = 0, 0;
sub save_data(*%data) {
for %data.values -> $data {
my $fh = open $data<filepath>, :r
or die "Something screwy with {$data<filepath>}: $!";
my @throw-away = $fh.lines(4);
my @rest = $fh.lines;
# RAKUDO: No &seek, no &truncate
$fh.close;
$fh = open $data<filepath>, :w
or die "Couldn't open {$data<filepath>} for writing: $!";
for $data<name vers stat>.list, $data<costs>.list, @rest -> $line {
print $fh: $line;
}
}
}
my %data = load_data( filename=>'weblog', version=>1 );
constant $is_active_bit = 0x0080;
for keys %data -> my $file {
my @costs;
given $data{$file} {
say "$file contains data on {.<name>}";
.<stat> +^= $is_active_bit;
@costs := .<costs>;
}
my $inflation;
$inflation = prompt 'Inflation rate: '
until $inflation != NaN;
@costs = sort { amortize($_) }, @costs >>*>> $inflation;
say 'Total expenditure: ', [+] @costs;
say 'Major expenditure: ', [+] grep * >= 1000, @costs;
say 'Minor expenditure: ', [+] grep * < 1000, @costs;
say 'Odd expenditures: ', @costs[1, 3 ... +@costs];
}
save_data(|%data, log => {name=>'metalog', vers=>1, costs=>[], stat=>0});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment