Paste: Discounted payback period

Author: Loryn Jenkins
Mode: factor
Date: Thu, 4 Apr 2013 10:05:49
Plain Text |
:: discount-value ( rate period payment -- discounted-value )
    1.0 rate 1.0 + period ^ / payment * ; 
    
:: discounted-payback-when ( rate investment cashflows -- period ) 
    cashflows values [ :> ( pmt i ) rate i 1 + pmt discount-value ] map-index 
    summation [ investment >= ] find drop ;

Annotation: A non-locals version of discount-value

Author: Loryn Jenkins
Mode: factor
Date: Thu, 4 Apr 2013 10:19:56
Plain Text |
: discount-value2 ( rate period payment -- discounted-value )
    [ 1 + ] 2dip [ ^ ] dip [ 1.0 swap / ] dip * ; 

Annotation: Two more attempts at non-locals versions

Author: Loryn Jenkins
Mode: factor
Date: Thu, 4 Apr 2013 10:31:54
Plain Text |
: discount-value3 ( rate period payment -- discounted-value )
    spin 1 + swap ^ 1.0 swap / * ;
    
: discount-value4 ( rate period payment -- discounted-value )
    -rot [ 1 + ] dip ^ 1.0 swap / * ;

Annotation: Version of no.2 with the rate adjustment factored out

Author: Loryn Jenkins
Mode: factor
Date: Thu, 4 Apr 2013 10:45:26
Plain Text |
: discount-value2 ( rate period payment -- discounted-value )
    [ ^ ] dip [ 1.0 swap / ] dip * ;

:: discounted-payback-when ( rate investment cashflows -- period ) 
    cashflows values [ :> ( pmt i ) rate i [ 1 + ] bi@ pmt discount-value2 ] map-index 
    summation [ investment >= ] find drop ;

Annotation: I guess fry wins!

Author: Loryn Jenkins
Mode: factor
Date: Thu, 4 Apr 2013 13:58:30
Plain Text |
I reckon this is the clearest version of them all. Fry expresses the intent well, and is more compact than the locals version.

: discount-value5 ( rate period payment -- discounted-value )
    '[ 1.0 _ _ ^ / _ * ] call ;

Annotation: another idea

Author: mrjbq7
Mode: factor
Date: Thu, 4 Apr 2013 21:53:55
Plain Text |
: discount-value6 ( rate period payment -- discounted-value )
    [ ^ recip ] [ * ] bi* ;

Annotation: infix

Author: mrjbq7
Mode: factor
Date: Thu, 4 Apr 2013 22:08:07
Plain Text |
:: discount-value7 ( rate period payment -- discounted-value )
    [infix (1.0 / (rate ** period)) * payment infix] ;

New Annotation

Summary:
Author:
Mode:
Body: