Files
RustAst/examples/HMA.myc
T
Brummel 8aa2a67b5b Introduce StaticType::Variadic for variadic parameters
This commit introduces `StaticType::Variadic` to represent variadic
parameter types in function signatures. This allows for more precise
type checking of functions that accept a variable number of arguments,
such as arithmetic operators.

Previously, variadic functions were handled using `StaticType::Any`,
which was too permissive and could lead to runtime errors. The new
`Variadic` type enforces that all arguments must conform to a specified
inner type.

Changes include:

- Adding `StaticType::Variadic` to `src/ast/types.rs`.
- Updating the type checker to handle `Variadic` types correctly when
  unifying with tuples or single arguments.
- Modifying built-in functions like arithmetic operators to use
  `Variadic` for their parameter types.
- Improving error messages for function call mismatches to be more
  specific.
- Adding a new test case to ensure arithmetic type mismatches are caught
  at compile time.
- A new example `data_stream_pipe_buffered.myc` is added.
- The `HMA.myc` example now has a `Skip: output` directive.
2026-03-30 11:36:26 +02:00

72 lines
2.1 KiB
Plaintext

;; Benchmark: 98.9us
;; Benchmark-Repeat: 22
;; Skip: output
(do
;; make-sma Factory (O(1))
(def make-sma
(fn [lookback]
(do
(def s (series lookback))
(def running-sum 0.0)
(def count 0)
(fn [x]
(do
(if (< count lookback)
(do (assign running-sum (+ running-sum x)) (assign count (+ count 1)))
(do (assign running-sum (+ (- running-sum (s (- lookback 1))) x))))
(push s x)
(/ running-sum count))))))
;; make-wma Factory (O(1))
(def make-wma
(fn [lookback]
(do
;; Sicherstellen, dass lookback mindestens 1 ist
(def n (if (< lookback 1) 1 lookback))
(def s (series n))
(def price-sum 0.0)
(def weighted-sum 0.0)
(def count 0)
(def weight-total (/ (* n (+ n 1)) 2))
(fn [x]
(do
(if (< count n)
(do
(assign count (+ count 1))
(assign price-sum (+ price-sum x))
(assign weighted-sum (+ weighted-sum (* x count)))
(push s x)
(/ weighted-sum (/ (* count (+ count 1)) 2)))
(do
(def oldest (s (- n 1)))
(assign weighted-sum (+ (- weighted-sum price-sum) (* x n)))
(assign price-sum (+ (- price-sum oldest) x))
(push s x)
(/ weighted-sum weight-total))))))))
;; make-hma Factory (O(1))
;; Hull Moving Average: WMA(sqrt(n), 2*WMA(n/2) - WMA(n))
(def make-hma
(fn [lookback]
(do
(def n lookback)
(def wma1 (make-wma (trunc (/ n 2))))
(def wma2 (make-wma n))
(def wma-final (make-wma (trunc (sqrt n))))
(fn [x]
(do
(def v1 (wma1 x))
(def v2 (wma2 x))
(wma-final (- (* 2.0 v1) v2)))))))
;; Beispiele & Test
(def hma16 (make-hma 16))
(print "HMA (16) Initialisierung gestartet...")
(def i 1.0)
(while (<= i 20.0)
(do
(print "HMA Step" i ":" (hma16 (* i 10.0)))
(assign i (+ i 1))))
)