Simple newbie question in Clojure...
If I have an odd number of elements in a Clojure vector, how can I extract the "middle" value? I've been looking at this for a while and can't work out how to do it!
Some examples:
(middle-value [0])
should return [0]
(middle-value [0 1 2])
should return [1]
(middle-value [0 1 :abc 3 4])
should return [:abc]
(middle-value [0 1 2 "test" 4 5 6])
should return ["test"]
How about calculating the middle index and accessing by it?
(defn middle-value [vect]
(when-not (empty? vect)
(vect (quot (count vect) 2))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With