! Copyright (C) 2008 Alex Chapman
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors calendar kernel math math.order money sequences ;
+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 ;
: process-day ( account date -- )
2dup accumulate-interest ?pay-interest ;
-: each-day ( quot: ( -- ) start end -- )
+: each-day ( ... quot: ( ... day -- ... ) start end -- ... )
2dup before? [
[ dup [ over [ swap call ] dip ] dip 1 days time+ ] dip each-day
] [
: 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 ;