Fix: Improve series type handling and coercions
This commit enhances the series type handling in `src/ast/rtl/series.rs` by introducing helper methods `as_float` and `as_int` in `src/ast/types.rs` to manage type conversions more robustly. It also updates the function overload resolution in `src/ast/types.rs` to prioritize exact parameter matches before falling back to implicit coercions. Finally, the example `err.myc` has been updated to reflect a more accurate implementation of a Simple Moving Average (SMA) calculation.
This commit is contained in:
+22
-23
@@ -1,25 +1,24 @@
|
||||
(do
|
||||
(def SMA
|
||||
(fn [length]
|
||||
(do
|
||||
(def history (series :float))
|
||||
(push history 0)
|
||||
|
||||
(fn [val]
|
||||
(do
|
||||
(def sum (+ val (history 0)))
|
||||
(push history sum)
|
||||
(/ sum length)
|
||||
(history 0)
|
||||
))
|
||||
)))
|
||||
|
||||
(def sma20 (SMA 20))
|
||||
|
||||
(def n 100)
|
||||
(while (> n 0)
|
||||
(do
|
||||
(print "val=" n " sma20=" (sma20 n))
|
||||
(assign n (- n 1))
|
||||
))
|
||||
(def SMA
|
||||
(fn [length]
|
||||
(do
|
||||
(def history (series :float))
|
||||
(push history 0)
|
||||
|
||||
(fn [val]
|
||||
(do
|
||||
(def sum (+ val (history 0)))
|
||||
(push history sum)
|
||||
(/ sum length)
|
||||
))
|
||||
)))
|
||||
|
||||
(def sma20 (SMA 20))
|
||||
|
||||
(def n 100)
|
||||
(while (> n 0)
|
||||
(do
|
||||
(print "val=" n " sma20=" (sma20 n))
|
||||
(assign n (- n 1))
|
||||
))
|
||||
)
|
||||
Reference in New Issue
Block a user