Clojure

sequence with transducers realizes n+2 elements

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: Release 1.7
  • Fix Version/s: Release 1.7
  • Component/s: None
  • Labels:
    None
  • Approval:
    Ok

Description

The first element is realized at creation time:

user=> (def a (sequence (take 2) (map #(do (println (str "~" %)) %) (iterate inc 1))))
~1
#'user/a

Fully realizing the sequence realizes the other n-1 elements, and 2 more:

user=> a
(~2
~3
1 ~4
2)

Compare with version using seq operations:

user=> (def a (sequence (take 2 (map #(do (println (str "~" %)) %) (iterate inc 1)))))
#'user/a
user=> a
(~1
~2
1 2)

Transduce also doesn't seem to exhibit this issue:

user=> (def a (transduce (take 2) conj [] (map #(do (println (str "~" %)) %) (iterate inc 1))))
~1
~2
#'user/a
user=> a
[1 2]
  1. clj-1497.diff
    08/Aug/14 10:01 AM
    0.9 kB
    Alex Miller
  2. clj-1497v2.diff
    08/Aug/14 10:16 AM
    2 kB
    Nicola Mometto

Activity

Hide
Alex Miller added a comment -

Patch attached that improves the issue - will now only realize n+1 elements.

Show
Alex Miller added a comment - Patch attached that improves the issue - will now only realize n+1 elements.
Hide
Nicola Mometto added a comment -

Nice, I added a commit on top of yours to delay the realization of the first element of the lazyseq to the first .next call instead of on SeqIteration creation

Show
Nicola Mometto added a comment - Nice, I added a commit on top of yours to delay the realization of the first element of the lazyseq to the first .next call instead of on SeqIteration creation
Hide
Alex Miller added a comment -

Fixed by Rich directly, not by patch.

Show
Alex Miller added a comment - Fixed by Rich directly, not by patch.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: