Share your thoughts in the 2024 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

I'm producing more than one element on the 1-arity of the transducer, and sequence is only considering the last one.

Here is the transducer and the tests that fail for sequence:

`
(defn sliding-window [n]
(fn [rf]

(let [a #js []]
  (fn
    ([] (rf))
    ([result]
     (loop [] ;; Here I'm emitting more than one element
       (when (not-empty a)
         (rf result (vec (js->clj a)))
         (.shift a)
         (recur))))
    ([result input]
     (.push a input)
     (if (== n (.-length a))
       (let [v (vec (js->clj a))]
         (.shift a)
         (rf result v))
       result))))))

;;This test fails! =(
(deftest sliding-window-in-a-sequence
(is (= [[5 4 3]

      [4 3 2]
      [3 2 1]
      [2 1]
      [1]]
     (sequence (sliding-window 3) [5 4 3 2 1])))

(is (= [[2 1]

      [1]]
     (sequence (sliding-window 3) [2 1]))))

`

7 Answers

0 votes
by

Comment made by: lucascs

I could make it work by recurring on the result.

`
([result]
(loop [res result]

(if (not-empty a)
  (let [v (vec (js->clj a))]
    (.shift a)
    (recur (rf res v)))
  res)))```

even so it's weird that the previous version behaves differently on core.async and sequences in cljs and clj

0 votes
by

Comment made by: dnolen

Please demonstrate the problem without core.async. Thanks.

0 votes
by

Comment made by: lucascs

Hi,

the last test I posted on the ticket, fails in cljs, but not in clj:

`
;;This test fails! =(
(deftest sliding-window-in-a-sequence
(is (= [[5 4 3]

      [4 3 2]
      [3 2 1]
      [2 1]
      [1]]
     (sequence (sliding-window 3) [5 4 3 2 1])))

(is (= [[2 1]

      [1]]
     (sequence (sliding-window 3) [2 1]))))

`

0 votes
by

Comment made by: dnolen

I've removed the core.async bits from the description to clarify the issue.

0 votes
by

Comment made by: dnolen

The implementation of {{sliding-window}} above does not appear to be correct, it doesn't return the result. This ticket needs more information.

0 votes
by

Comment made by: lucascs

As I said on http://dev.clojure.org/jira/browse/CLJS-1222?focusedCommentId=38620&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-38620

changing the 1-arity of the {{sliding-window}} to that fixes the transducer.

The point of this ticket now is that the behavior of the same (wrong) transducer in clj (both core.async and sequence) and cljs (core.async) is different than cljs sequence.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-1222 (reported by alex+import)
...