-USING: accessors calendar kernel math math.order money sequences ;
+! Copyright (C) 2008 Alex Chapman
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors calendar kernel math math.order sequences ;
IN: bank
TUPLE: account name interest-rate interest-payment-day opening-date transactions unpaid-interest interest-last-paid ;
over transactions>> push ;
: total ( transactions -- balance )
- 0 [ amount>> + ] reduce ;
+ [ amount>> ] map-sum ;
: balance>> ( account -- balance ) transactions>> total ;
: open-account ( name interest-rate interest-payment-day opening-date opening-balance -- account )
- >r [ <account> ] keep r> "Account Opened" <transaction> >>transaction ;
+ [ [ <account> ] keep ] dip "Account Opened" <transaction> >>transaction ;
: daily-rate ( yearly-rate day -- daily-rate )
days-in-year / ;
: process-day ( account date -- )
2dup accumulate-interest ?pay-interest ;
-: each-day ( quot start end -- )
+: each-day ( ... quot: ( ... day -- ... ) start end -- ... )
2dup before? [
- >r dup >r over >r swap call r> r> 1 days time+ r> each-day
+ [ dup [ over [ swap call ] dip ] dip 1 days time+ ] dip each-day
] [
3drop
- ] if ;
+ ] if ; inline recursive
: process-to-date ( account date -- account )
over interest-last-paid>> 1 days time+
- [ dupd process-day ] spin each-day ;
+ [ [ dupd process-day ] ] 2dip swap each-day ;
: inserting-transactions ( account transactions -- account )
[ [ date>> process-to-date ] keep >>transaction ] each ;