From 89eec0a86907a881410a5e5ac87cd8dc0268df3d Mon Sep 17 00:00:00 2001 From: dakkar Date: Sat, 25 Jan 2020 16:24:11 +0000 Subject: numeric types, not-zero constant --- lib/MaildirIndexer/Index/ByAddresses.rakumod | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/MaildirIndexer/Index/ByAddresses.rakumod b/lib/MaildirIndexer/Index/ByAddresses.rakumod index 3e7dc9d..678254e 100644 --- a/lib/MaildirIndexer/Index/ByAddresses.rakumod +++ b/lib/MaildirIndexer/Index/ByAddresses.rakumod @@ -20,6 +20,8 @@ has Int $!total-count; has Hash $!cached-p-given-mailbox .= new; has Instant $!last-cached-at .= from-posix(0); +constant $NOT-ZERO = 1e-15; + method dump() { } @@ -28,13 +30,13 @@ submethod account-for(Str @addresses,Str $mailbox,Int $step) { $!count-by-mailbox{$mailbox} += $step; for @addresses -> Str $addr { - my $pair = ( $addr => $mailbox ); + my Pair $pair = ( $addr => $mailbox ); $!known-addresses{$addr} += $step; - my $count = $!count-by-address-and-mailbox{$pair} += $step; + my Numeric $count = $!count-by-address-and-mailbox{$pair} += $step; if ($count) { - my $a = 1e-15 + $count; - my $b = 1 + $!count-by-mailbox{$mailbox}; + my Numeric $a = $NOT-ZERO + $count; + my Numeric $b = 1 + $!count-by-mailbox{$mailbox}; $!p-address-given-mailbox{$pair} = $a / $b; } else { @@ -45,10 +47,10 @@ submethod account-for(Str @addresses,Str $mailbox,Int $step) { # update the cache every 10 seconds if ((now - $!last-cached-at) > 10) { for $!count-by-mailbox.keys -> Str $mailbox { - my $p = $!count-by-mailbox{$mailbox} / $!total-count; + my Numeric $p = $!count-by-mailbox{$mailbox} / $!total-count; for $!known-addresses.keys -> Str $addr { - my $addr-p = $!p-address-given-mailbox{$addr => $mailbox} // 1e-15; + my $addr-p = $!p-address-given-mailbox{$addr => $mailbox} // $NOT-ZERO; $p *= 1 - $addr-p; } @@ -88,13 +90,13 @@ method del-path(IO:D $file, Str:D $mailbox --> Nil) { } submethod predict-mailbox-given-addresses(@addresses) { - my %prediction; + my Numeric %prediction; for $!count-by-mailbox.keys -> Str $mailbox { - my $p = $!cached-p-given-mailbox{$mailbox}; + my Numeric $p = $!cached-p-given-mailbox{$mailbox} // $NOT-ZERO; for @addresses -> Str $addr { - my $addr-p = $!p-address-given-mailbox{$addr => $mailbox} // 1e-15; + my $addr-p = $!p-address-given-mailbox{$addr => $mailbox} // $NOT-ZERO; $p *= $addr-p / ( 1- $addr-p ); } -- cgit v1.2.3