ClojureScript

"range" values are sometimes behaving unexpectedly

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    browsers, rhino

Description

The lazy sequences returned by "range" are, in some situations, producing unexpected results as elements of larger computations.

Example:

(defn range-print [rng]
  (let [fst (first rng)
        rst (rest rng)]
    (when fst
      (.log js/console "first: " (clj->js fst))
      (.log js/console "rest: " (clj->js rst))
      (range-print rst))))

(range-print (range 3))

(range-print (map identity (range 3)))

(comment

  (range-print (range 3))

  ;; In my browser's console, the output of the above expression looks as
  ;; follows. Note the UNexpected "first" at the end.

  first:  0
  rest:  [1, 2]
  first:  1
  rest:  [2]
  first:  2
  rest:  []
  first:  3
  rest:  []

  ;; --------------------------------------------------------------------------

  (range-print (map identity (range 3)))

  ;; The above expression generates the output I expect.

  first:  0
  rest:  [1, 2]
  first:  1
  rest:  [2]
  first:  2
  rest:  []

  )

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: