Skip to content

Instantly share code, notes, and snippets.

@michaelsbradleyjr
Last active December 30, 2015 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelsbradleyjr/7899410 to your computer and use it in GitHub Desktop.
Save michaelsbradleyjr/7899410 to your computer and use it in GitHub Desktop.
A bug in ClojureScript's range implementation?
(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))))
(comment
(range-print (range 10))
;; 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, 3, 4, 5, 6, 7, 8, 9]
first: 1
rest: [2, 3, 4, 5, 6, 7, 8, 9]
first: 2
rest: [3, 4, 5, 6, 7, 8, 9]
first: 3
rest: [4, 5, 6, 7, 8, 9]
first: 4
rest: [5, 6, 7, 8, 9]
first: 5
rest: [6, 7, 8, 9]
first: 6
rest: [7, 8, 9]
first: 7
rest: [8, 9]
first: 8
rest: [9]
first: 9
rest: []
first: 10
rest: []
;; --------------------------------------------------------------------------
(range-print (map identity (range 10)))
;; the above expression generates the output I expect.
first: 0
rest: [1, 2, 3, 4, 5, 6, 7, 8, 9]
first: 1
rest: [2, 3, 4, 5, 6, 7, 8, 9]
first: 2
rest: [3, 4, 5, 6, 7, 8, 9]
first: 3
rest: [4, 5, 6, 7, 8, 9]
first: 4
rest: [5, 6, 7, 8, 9]
first: 5
rest: [6, 7, 8, 9]
first: 6
rest: [7, 8, 9]
first: 7
rest: [8, 9]
first: 8
rest: [9]
first: 9
rest: []
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment