ClojureDocs

Nav

Namespaces

reverse

clojure.core

Available since 1.0 (source)
  • (reverse coll)
Returns a seq of the items in coll in reverse order. Not lazy.
3 Examples
user=> (reverse '(1 2 3))
(3 2 1)
(reverse "clojure")
; (\e \r \u \j \o \l \c)

(apply str (reverse "clojure"))
; "erujolc"
;; Comparison between `reverse` and `rseq` for a vector.

(def nums (vec (range 1000000)))
(time (reverse nums))
;; "Elapsed time: 30.1222 msecs"
(time (rseq nums))
;; "Elapsed time: 0.0664 msecs"
See Also

Returns, in constant time, a seq of the items in rev (which can be a vector or sorted-map), in rev...

Added by gstamp
2 Notes
    By , created 14.3 years ago

    If you've got a vector rseq is a good option instead of reverse.

    By , created 4.1 years ago, updated 4.1 years ago

    For vectors, rseq is to reverse what peek is to last. rseq reverses in constant time (!) where reverse is in linear time, as it appears to pull everything into a new seq, one at a time instead of ... reversing the keys? I don't know the implementation details, but I do know that rseq is magnitudes faster than reverse for vectors and sorted maps.