[up]

Math

Remember, that you can click on the code to execute it.


Well, the obligatory factorial example

    100 factorial


An interest rate calculator

    |initialAmount sep|

    initialAmount := 100 asFixedPoint:2.
    sep := '+---+'.
    1 to:20 do:[:year | sep := sep , '-------+'].
    Transcript showCR:sep.
    sep := '|   |'.
    1 to:20 do:[:year | sep := sep , '   %1  |' bindWith:(year printStringLeftPaddedTo:2)].
    Transcript showCR:sep.
    sep := '+---+'.
    1 to:20 do:[:year | sep := sep , '-------+'].
    Transcript showCR:sep.

    #(0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.10 0.20) do:[:percentF |
	|percent|
	percent := percentF asFixedPoint:2.
	Transcript show: '|'.
	Transcript show: ((percent * 100) asInteger printStringLeftPaddedTo:2); show: '%'.
	Transcript show: '|'.
	1 to:20 do:[:years |
	    |amount|
	    amount := initialAmount * ((1 + percent) raisedTo:years).
	    Transcript show: (amount printStringLeftPaddedTo:7).
	    Transcript show: '|'.
	].
	Transcript cr.
	Transcript showCR:sep.
    ].
BTW, this should tell you, why you should never lend money for more than a (say) 7% interest rate, and that CEOs promising a continued return on capital of 20% over many years are criminal liars ;-)
And, after 2008, we should add negative rates to the above table as well.


Replacing a string in all files recursively down a directory hierarchy

the following example is only a template.
Copy it to a workspace, replace '<<top-directory-name-here>>', '<<search>>', '<<original>>'
and '<<replacement>>' with appropriate strings. It will recursively scan find all files under a top-directory, search for lines containing a search string, and replace a given string by another there.
I used it the last time, when I had to edit 1000 files to change a CVS-package id.

    |topDir|

    topDir := '<<top-directory-name-here>>' asFilename.
    topDir recursiveDirectoryContentsDo:[:eachFile |
	|fn newContents|

	fn := topDir construct:eachFile.
	fn isDirectory ifFalse:[
	    newContents := fn contents
			collect:[:eachLine |
			    (eachLine includesString:'<<search>>') ifTrue:[
				eachLine copyReplaceString:'<<original>>' with:'<<replacement>>'
			    ] ifFalse:[
				eachLine
			    ]
			].
	    fn contents:newContents
	].
    ].


... I will add some of my secrets here soon ...


Back to "Using Workspaces".

[stx-logo]
Copyright © Claus Gittinger Development & Consulting
Copyright © eXept Software AG

<cg at exept.de>

Doc $Revision: 1.8 $ $Date: 2021/03/13 18:24:49 $