Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -1,54 +1,54 @@
|
|||
#define std'dictionary'*.
|
||||
#define std'basic'*.
|
||||
#define std'collections'*.
|
||||
#define std'patterns'*.
|
||||
#define system.
|
||||
#define system'routines.
|
||||
#define system'collections.
|
||||
#define extensions.
|
||||
|
||||
#class ESMARole
|
||||
#class SMAExtender
|
||||
{
|
||||
#field thePeriod.
|
||||
|
||||
#initializer : aPeriod
|
||||
#constructor new : aPeriod
|
||||
[
|
||||
thePeriod := aPeriod.
|
||||
]
|
||||
|
||||
#method + aNumber
|
||||
#method add : aNumber
|
||||
[
|
||||
self += NewInt32Value::aNumber.
|
||||
self += aNumber.
|
||||
|
||||
#if (self count > thePeriod)?
|
||||
[
|
||||
self first_item'reduce.
|
||||
].
|
||||
#var aCount := self length.
|
||||
^ aCount =>
|
||||
0 ? [ 0.0r ]
|
||||
! [
|
||||
(aCount > thePeriod)?
|
||||
[
|
||||
self remove &index:0.
|
||||
].
|
||||
|
||||
#var aCount := self count.
|
||||
#if (aCount == 0)?
|
||||
[ ^ 0. ].
|
||||
|
||||
#var aSum := Summing::Real::0 start:Scan::self.
|
||||
|
||||
^ aSum / aCount.
|
||||
#var aSum := Summing new:(Real new:0) foreach:self.
|
||||
^ aSum / self length.
|
||||
].
|
||||
]
|
||||
}
|
||||
|
||||
#symbol SMA : aPeriod = __wrap(ESMARole::aPeriod, List).
|
||||
#symbol sma = (:aPeriod) [ Extension(SMAExtender new:aPeriod, List new) ].
|
||||
|
||||
// --- Program ---
|
||||
|
||||
#symbol Program =
|
||||
#symbol program =
|
||||
[
|
||||
#var SMA3 := SMA::3.
|
||||
#var SMA5 := SMA::5.
|
||||
#var SMA3 := sma:3.
|
||||
#var SMA5 := sma:5.
|
||||
|
||||
loop &&from:1 &to:5 run: i =
|
||||
control from:1 &to:5 &do: i
|
||||
[
|
||||
'program'output << "sma3 + " << i << " = " << SMA3 + i.
|
||||
'program'output << ",sma5 + " << i << " = " << SMA5 + i << "%n".
|
||||
consoleEx writeLine:"sma3 + " :i :" = ": (SMA3 + i number).
|
||||
consoleEx writeLine:"sma5 + " :i :" = ": (SMA5 + i number).
|
||||
].
|
||||
|
||||
loop &&from:5 &to:1 &step:-1 run: i =
|
||||
control from:5 &backTo:1 &do: i
|
||||
[
|
||||
'program'output << "sma3 + " << i << " = " << SMA3 + i.
|
||||
'program'output << ",sma5 + " << i << " = " << SMA5 + i << "%n".
|
||||
consoleEx writeLine:"sma3 + " :i :" = ": (SMA3 + i number).
|
||||
consoleEx writeLine:"sma5 + " :i :" = ": (SMA5 + i number).
|
||||
].
|
||||
].
|
||||
|
|
|
|||
|
|
@ -1,10 +1,8 @@
|
|||
sub sma (Int $period where (* > 0)) returns Sub {
|
||||
my $sum = 0;
|
||||
my @a;
|
||||
return sub ($x) {
|
||||
@a.push: $x;
|
||||
$sum += $x;
|
||||
$sum -= @a.shift if @a > $period;
|
||||
return $sum / @a;
|
||||
}
|
||||
sub sma(Int $period where * > 0) returns Sub {
|
||||
sub ($x) {
|
||||
state @a = 0 xx $period;
|
||||
@a.push($x);
|
||||
@a.shift;
|
||||
$period R/ [+] @a;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue