ClojureScript

Sequence of a stateful transducer is producing the wrong answer

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: 1.7.145
  • Fix Version/s: 1.9.671
  • Component/s: None
  • Labels:
  • Environment:
    OSX 10.10.3, java 1.8.0-ea-b124

Description

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]))))

Activity

Hide
Lucas Cavalcanti added a comment -

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

Show
Lucas Cavalcanti added a comment - 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
Hide
David Nolen added a comment -

Please demonstrate the problem without core.async. Thanks.

Show
David Nolen added a comment - Please demonstrate the problem without core.async. Thanks.
Hide
Lucas Cavalcanti added a comment -

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]))))
Show
Lucas Cavalcanti added a comment - 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]))))
Hide
David Nolen added a comment -

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

Show
David Nolen added a comment - I've removed the core.async bits from the description to clarify the issue.
Hide
David Nolen added a comment -

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

Show
David Nolen added a comment - The implementation of sliding-window above does not appear to be correct, it doesn't return the result. This ticket needs more information.
Hide
Lucas Cavalcanti added a comment -

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.

Show
Lucas Cavalcanti added a comment - 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.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated: