From 0f5a4a72ac5c34a153a2534fbf32ae55c27109a1 Mon Sep 17 00:00:00 2001 From: Zach Smith Date: Mon, 17 May 2021 20:17:30 -0400 Subject: [PATCH] Regularlize get-shape --- src/calc.janet | 22 +++++++++++++++------- src/operations.janet | 2 +- src/repl.janet | 10 +++++----- test/calc.janet | 4 ++-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/calc.janet b/src/calc.janet index 5321171..758e62a 100644 --- a/src/calc.janet +++ b/src/calc.janet @@ -3,7 +3,10 @@ (defproto Stack () env {} data {}) -(defmethod _init Stack [self] (put self :data @[])) + +(defmethod _init Stack + [self] + (put self :data @[])) (defproto Element () doc {:default ""}) @@ -27,7 +30,7 @@ type {:allocate-value :quotation}) (defproto Vector Quotation - shape {:default [] :init? true} + shape {:default [] :init? true :getter false} data {:init? true} type {:allocate-value :vector}) @@ -54,7 +57,7 @@ ((table/getproto elem) :_name))) (defmethod get-shape Number [self] []) -(defmethod get-shape Vector [self] (shape self)) +(defmethod get-shape Vector [self] (self :shape)) (defmethod fill Number [element shape-to-fill] @@ -67,7 +70,7 @@ (defmethod fill Vector [self shape-to-fill] - (let [x (array ;(shape self)) + (let [x (array ;(get-shape self)) y (array ;shape-to-fill)] (while (not (empty? x)) @@ -75,10 +78,10 @@ yi (array/pop y)] (unless (= xi yi) (errorf "Shape error: can't fill vector with shape %q to %q" - (shape self) shape-to-fill)))) + (get-shape self) shape-to-fill)))) (reduce (fn [acc length] - (let [new-shape (tuple length ;(shape acc)) + (let [new-shape (tuple length ;(get-shape acc)) new-data (array/new-filled length acc)] (new-Vector new-shape new-data))) self @@ -86,7 +89,11 @@ # TODO: Do we want to do something more sophisticated here? (declare-open-multi make-element) -(extend-multi make-element [:number] [n] (new-Float n :doc (string n))) + +(extend-multi make-element [:number] + [n] + (new-Float n :doc (string n))) + (extend-multi make-element [:boolean] [b] (if b @@ -106,6 +113,7 @@ (errorf "Vector error: can't wrap heterogeneous data. Expected shape %j, got %j." inner-shape (get-shape datum)))) + (new-Vector (tuple (length data) ;inner-shape) data))) (defmethod pop Stack diff --git a/src/operations.janet b/src/operations.janet index 4469999..a255a4c 100644 --- a/src/operations.janet +++ b/src/operations.janet @@ -11,7 +11,7 @@ (let [x (first args)] (if (calc/Vector? x) (calc/new-Vector - (x :shape) + (calc/get-shape x) (map (partial inner-apply op) ;(map |(@ calc/Vector $ :data) args))) (let [f (fun-ref op) unwrapped (map |(calc/value $) args)] diff --git a/src/repl.janet b/src/repl.janet index 8eef21b..0c0ecb6 100644 --- a/src/repl.janet +++ b/src/repl.janet @@ -120,11 +120,11 @@ (parser/parse) (handle-commands (@))) - ([err fib] - (eprint err) - (if (os/getenv "EC_TRACEBACK") - (propagate err fib) - (set (@ data) bak))))))) + ([err fib] + (eprint err) + (if (os/getenv "EC_TRACEBACK") + (propagate err fib) + (set (@ data) bak))))))) (defn handle-lines ``` diff --git a/test/calc.janet b/test/calc.janet index c3c3e69..f9c437a 100644 --- a/test/calc.janet +++ b/test/calc.janet @@ -105,7 +105,7 @@ (calc/push s c/wrap-all) (def vec (calc/pop s)) - (is (= [2] (calc/shape vec))) + (is (= [2] (calc/get-shape vec))) (is (empty? (s :data)))) (deftest wrap-quotations @@ -115,7 +115,7 @@ (calc/push s c/wrap-all) (def vec (calc/pop s)) - (is (= [2 2] (calc/shape vec))) + (is (= [2 2] (calc/get-shape vec))) (is (empty? (s :data)))) (deftest wrap-failure -- 2.38.5