From 66ec0a1ebd3a443a55eb01bc8dc5b0f8e4f4235a Mon Sep 17 00:00:00 2001 From: Nic McPhee Date: Thu, 17 Dec 2020 18:45:06 -0600 Subject: [PATCH] Address the possibility of vectors being empty in `vector/_first` The original version just used the built-in `first`, which returns `nil` if you give it an empty collection, which is almost certainly not a useful behavior. This changes it to return `:ignore-instruction` if the vector is empty, thereby leaving all the stacks unchanged. --- src/propeller/push/instructions/vector.cljc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/propeller/push/instructions/vector.cljc b/src/propeller/push/instructions/vector.cljc index eb299b2..ae2ea99 100755 --- a/src/propeller/push/instructions/vector.cljc +++ b/src/propeller/push/instructions/vector.cljc @@ -52,12 +52,16 @@ (make-instruction state empty? [stack] :boolean))) ;; Pushes the first item of the top element of the vector stack onto the -;; approrpiately-typed literal stack +;; approrpiately-typed literal stack. If the vector is empty, return +;; :ignore-instruction so that nothing is changed on the stacks. (def _first ^{:stacks #{:elem}} (fn [stack state] (let [lit-stack (get-vector-literal-type stack)] - (make-instruction state first [stack] lit-stack)))) + (make-instruction state + #(if (empty? %) :ignore-instruction (first %)) + [stack] + lit-stack)))) ;; Pushes onto the INTEGER stack the index of the top element of the ;; appropriately-typed literal stack within the top element of the vector stack