Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reverse a vector in clojure (noob)

Tags:

clojure

Hi I just starting with Clojure and I cannot manage to reverse a vector of integers.

;generate a vector of random integers
(defn generate-rands
  [x]
  (vector (take x (repeatedly #(rand-int 100))))
  )

;vector of integers
(def my-vector (generate-rands 10))

;reverse the vector of integers
(def my-vector-reversed (reverse my-vector))

;main
(defn main-app
  []  
  (println "My Vector: \t\t\t" my-vector)
  (println "My Vector Reversed: \t" my-vector-reversed))

The output is

=> (main-app)
My Vector:           [(14 49 29 3 66 7 60 60 34 19)]
My Vector Reversed:  [((14 49 29 3 66 7 60 60 34 19))]
nil
#'startingclojure.app/main-app

=> (vector? my-vector-reversed)
false

Can someone kindly explain me why my-vector-reversed is not a vector? And how can I reverse the content of 'my-vector'? Thanks

like image 470
nuvio Avatar asked Nov 17 '13 12:11

nuvio


1 Answers

Also, it's preferable to use rseq instead of reverse when you work with vector or sorted-map. It has constant time, because they are indexed and can be efficiently walked in either direction.

like image 182
Patison Avatar answered Oct 21 '22 01:10

Patison