June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -23,19 +23,19 @@ class SMA
! [
if (aCount > thePeriod)
[
theList remove at:0.
theList removeAt:0.
aCount := thePeriod
].
var aSum := theList summarize(Real new int:0).
var aSum := theList summarize(Real new).
^ aSum / aCount
]
]
}
program =
public program =
[
var SMA3 := SMA new:3.
var SMA5 := SMA new:5.

View file

@ -0,0 +1,22 @@
function movingaverage(::Type{T} = Float64; lim::Integer = -1) where T<:Real
buffer = Vector{T}(0)
if lim == -1
# unlimited buffer
return (y::T) -> begin
push!(buffer, y)
return mean(buffer)
end
else
# limited size buffer
return (y) -> begin
push!(buffer, y)
if length(buffer) > lim shift!(buffer) end
return mean(buffer)
end
end
end
test = movingaverage()
@show test(1.0) # mean([1])
@show test(2.0) # mean([1, 2])
@show test(3.0) # mean([1, 2, 3])

View file

@ -0,0 +1,14 @@
sub sma-generator (Int $P where * > 0) {
sub ($x) {
state @a = 0 xx $P;
@a.push($x).shift;
@a.sum / $P;
}
}
# Usage:
my &sma = sma-generator 3;
for 1, 2, 3, 2, 7 {
printf "append $_ --> sma = %.2f (with period 3)\n", sma $_;
}