Paste: htmlize-file

Author: kenanb
Mode: html
Date: Thu, 17 Nov 2011 21:19:27
Plain Text |
<?xml version="1.0" encoding="UTF-8"?><html>
        <head>
            <style>.NULL {
color: #000000;
}
.COMMENT1 {
color: #cc0000;
}
.COMMENT2 {
color: #ff8400;
}
.COMMENT3 {
color: #6600cc;
}
.COMMENT4 {
color: #cc6600;
}
.DIGIT {
color: #ff0000;
}
.FUNCTION {
color: #9966ff;
}
.INVALID {
background: #ffffcc;
color: #ff0066;
}
.KEYWORD1 {
color: #006699;
font-weight: bold;
}
.KEYWORD2 {
color: #009966;
font-weight: bold;
}
.KEYWORD3 {
color: #0099ff;
font-weight: bold;
}
.KEYWORD4 {
color: #66ccff;
font-weight: bold;
}
.LABEL {
color: #02b902;
}
.LITERAL1 {
color: #ff00cc;
}
.LITERAL2 {
color: #cc00cc;
}
.LITERAL3 {
color: #9900cc;
}
.LITERAL4 {
color: #6600cc;
}
.MARKUP {
color: #0000ff;
}
.OPERATOR {
color: #000000;
font-weight: bold;
}
</style>
            <title>resource:extra/bank/bank.factor</title>
        </head>
        <body>
            <pre><span class="COMMENT1">! </span><span class="COMMENT1">Copyright</span><span class="COMMENT1"> </span><span class="COMMENT1">(C)</span><span class="COMMENT1"> </span><span class="COMMENT1">2008</span><span class="COMMENT1"> </span><span class="COMMENT1">Alex</span><span class="COMMENT1"> </span><span class="COMMENT1">Chapman</span>

<span class="COMMENT1">! </span><span class="COMMENT1">See</span><span class="COMMENT1"> </span><span class="COMMENT1">http://factorcode.org/license.txt</span><span class="COMMENT1"> </span><span class="COMMENT1">for</span><span class="COMMENT1"> </span><span class="COMMENT1">BSD</span><span class="COMMENT1"> </span><span class="COMMENT1">license.</span>
<span class="MARKUP">USING: </span><span class="LITERAL2">accessors</span><span class="LITERAL2"> </span><span class="LITERAL2">calendar</span><span class="LITERAL2"> </span><span class="LITERAL2">kernel</span><span class="LITERAL2"> </span><span class="LITERAL2">math</span><span class="LITERAL2"> </span><span class="LITERAL2">math.order</span><span class="LITERAL2"> </span><span class="LITERAL2">money</span><span class="LITERAL2"> </span><span class="LITERAL2">sequences</span><span class="LITERAL2"> </span><span class="MARKUP">;</span>

<span class="MARKUP">IN: bank</span>

TUPLE: account name interest-rate interest-payment-day opening-date transactions unpaid-interest interest-last-paid <span class="MARKUP">;</span>

<span class="MARKUP">: &lt;account&gt;</span> <span class="COMMENT3">( </span><span class="COMMENT4">name</span><span class="COMMENT4"> </span><span class="COMMENT4">interest</span><span class="COMMENT4">-</span><span class="COMMENT4">rate</span><span class="COMMENT4"> </span><span class="COMMENT4">interest</span><span class="COMMENT4">-</span><span class="COMMENT4">payment</span><span class="COMMENT4">-</span><span class="COMMENT4">day</span><span class="COMMENT4"> </span><span class="COMMENT4">opening</span><span class="COMMENT4">-</span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    V<span class="OPERATOR">{</span> <span class="OPERATOR">}</span> clone <span class="DIGIT">0</span> pick account boa <span class="MARKUP">;</span>

TUPLE: transaction date amount description <span class="MARKUP">;</span>
C: &lt;transaction&gt; transaction


<span class="MARKUP">: &gt;&gt;transaction</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">transaction</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>
    over transactions&gt;&gt; push <span class="MARKUP">;</span>

<span class="MARKUP">: total</span> <span class="COMMENT3">( </span><span class="COMMENT4">transactions</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">balance</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>
    <span class="DIGIT">0</span> <span class="OPERATOR">[</span> amount&gt;&gt; + <span class="OPERATOR">]</span> reduce <span class="MARKUP">;</span>

<span class="MARKUP">: balance&gt;&gt;</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">balance</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span> transactions&gt;&gt; total <span class="MARKUP">;</span>

<span class="MARKUP">: open-account</span> <span class="COMMENT3">( </span><span class="COMMENT4">name</span><span class="COMMENT4"> </span><span class="COMMENT4">interest</span><span class="COMMENT4">-</span><span class="COMMENT4">rate</span><span class="COMMENT4"> </span><span class="COMMENT4">interest</span><span class="COMMENT4">-</span><span class="COMMENT4">payment</span><span class="COMMENT4">-</span><span class="COMMENT4">day</span><span class="COMMENT4"> </span><span class="COMMENT4">opening</span><span class="COMMENT4">-</span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT4">opening</span><span class="COMMENT4">-</span><span class="COMMENT4">balance</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    <span class="OPERATOR">[</span> <span class="OPERATOR">[</span> &lt;account&gt; <span class="OPERATOR">]</span> keep <span class="OPERATOR">]</span> dip <span class="LITERAL1">"</span><span class="LITERAL1">Account</span><span class="LITERAL1"> </span><span class="LITERAL1">Opened</span><span class="LITERAL1">"</span> &lt;transaction&gt; &gt;&gt;transaction <span class="MARKUP">;</span>

<span class="MARKUP">: daily-rate</span> <span class="COMMENT3">( </span><span class="COMMENT4">yearly</span><span class="COMMENT4">-</span><span class="COMMENT4">rate</span><span class="COMMENT4"> </span><span class="COMMENT4">day</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">daily</span><span class="COMMENT4">-</span><span class="COMMENT4">rate</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    days-in-year / <span class="MARKUP">;</span>

<span class="MARKUP">: daily-rate&gt;&gt;</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">rate</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    <span class="OPERATOR">[</span> interest-rate&gt;&gt; <span class="OPERATOR">]</span> dip daily-rate <span class="MARKUP">;</span>

<span class="MARKUP">: transactions-on-date</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">transactions</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    <span class="OPERATOR">[</span> before? <span class="OPERATOR">]</span> curry filter <span class="MARKUP">;</span>

<span class="MARKUP">: balance-on-date</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">balance</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    transactions-on-date total <span class="MARKUP">;</span>

<span class="MARKUP">: pay-interest</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>
    over unpaid-interest&gt;&gt; <span class="LITERAL1">"</span><span class="LITERAL1">Interest</span><span class="LITERAL1"> </span><span class="LITERAL1">Credit</span><span class="LITERAL1">"</span> &lt;transaction&gt;

    &gt;&gt;transaction <span class="DIGIT">0</span> &gt;&gt;unpaid-interest drop <span class="MARKUP">;</span>

<span class="MARKUP">: interest-payment-day?</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">?</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    day&gt;&gt; swap interest-payment-day&gt;&gt; = <span class="MARKUP">;</span>

<span class="MARKUP">: ?pay-interest</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    2dup interest-payment-day? <span class="OPERATOR">[</span> pay-interest <span class="OPERATOR">]</span> <span class="OPERATOR">[</span> 2drop <span class="OPERATOR">]</span> if <span class="MARKUP">;</span>

<span class="MARKUP">: unpaid-interest+</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">amount</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    over unpaid-interest&gt;&gt; + &gt;&gt;unpaid-interest <span class="MARKUP">;</span>

<span class="MARKUP">: accumulate-interest</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    <span class="OPERATOR">[</span> dupd daily-rate&gt;&gt; over balance&gt;&gt; * unpaid-interest+ <span class="OPERATOR">]</span> keep
    &gt;&gt;interest-last-paid drop <span class="MARKUP">;</span>

<span class="MARKUP">: process-day</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    2dup accumulate-interest ?pay-interest <span class="MARKUP">;</span>

<span class="MARKUP">: each-day</span> <span class="COMMENT3">( </span><span class="COMMENT4">.</span><span class="COMMENT4">.</span><span class="COMMENT4">.</span><span class="COMMENT4"> </span><span class="COMMENT4">quot</span><span class="COMMENT4">:</span><span class="COMMENT4"> </span><span class="COMMENT4">(</span><span class="COMMENT4"> </span><span class="COMMENT4">.</span><span class="COMMENT4">.</span><span class="COMMENT4">.</span><span class="COMMENT4"> </span><span class="COMMENT4">day</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">.</span><span class="COMMENT4">.</span><span class="COMMENT4">.</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span> start end -- ... )
    2dup before? <span class="OPERATOR">[</span>

        <span class="OPERATOR">[</span> dup <span class="OPERATOR">[</span> over <span class="OPERATOR">[</span> swap call <span class="OPERATOR">]</span> dip <span class="OPERATOR">]</span> dip <span class="DIGIT">1</span> days time+ <span class="OPERATOR">]</span> dip each-day
    <span class="OPERATOR">]</span> <span class="OPERATOR">[</span>

        3drop
    <span class="OPERATOR">]</span> if <span class="MARKUP">;</span> inline recursive

<span class="MARKUP">: process-to-date</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">date</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>

    over interest-last-paid&gt;&gt; <span class="DIGIT">1</span> days time+
    <span class="OPERATOR">[</span> <span class="OPERATOR">[</span> dupd process-day <span class="OPERATOR">]</span> <span class="OPERATOR">]</span> 2dip swap each-day <span class="MARKUP">;</span>

<span class="MARKUP">: inserting-transactions</span> <span class="COMMENT3">( </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT4">transactions</span><span class="COMMENT4"> </span><span class="COMMENT3">--</span><span class="COMMENT4"> </span><span class="COMMENT4">account</span><span class="COMMENT4"> </span><span class="COMMENT3">)</span>
    <span class="OPERATOR">[</span> <span class="OPERATOR">[</span> date&gt;&gt; process-to-date <span class="OPERATOR">]</span> keep &gt;&gt;transaction <span class="OPERATOR">]</span> each <span class="MARKUP">;</span></pre>

        </body>
    </html>

New Annotation

Summary:
Author:
Mode:
Body: