Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

caddr in Clojure

Tags:

jvm

clojure

lisp

What is the equivalent of Lisp's caddr in Clojure?

like image 374
Chiron Avatar asked Mar 04 '11 02:03

Chiron


3 Answers

caddr in Lisps is often used in a manner that amounts to destructuring. Clojure has ubiquitous support for destructuring, so caddr is less useful.

  (let [[_ _ x] [1 2 3 4]]
    x) ;; -> 3

EDIT: In response to @4bu3li.

(defn describe-path [[first :as edge]] 
  `(there is a ~(last edge) going ~first from here.))

There is no way to specify the last element w/ destructuring, but that's not really related to the original question anyway.

like image 140
dnolen Avatar answered Oct 10 '22 00:10

dnolen


As caddr is equivalent to third in Common Lisp, the closest I see in Clojure is nth:

(nth collection 2)

It's more generic than caddr, in that it aspires to work on more structures that cons cell chains. Note, though, that's it's not a place as it is in Common Lisp.

like image 20
seh Avatar answered Oct 10 '22 01:10

seh


Usually when I need to map a concept from one language to another, I check http://hyperpolyglot.org/

Clojure is included on the Lisp page: http://hyperpolyglot.org/lisp

From this, it appears that there is no direct analogue to caddr.

like image 8
tildedave Avatar answered Oct 10 '22 01:10

tildedave