[ANN] cadr: `car', ..., `cddddr' in Clojure

184 views
Skip to first unread message

Peter Danenberg

unread,
Nov 29, 2011, 6:09:49 PM11/29/11
to clo...@googlegroups.com
Try as I might, I can't purge these anachronisms; so here are `car' to
`cddddr' in all their glory:

http://clojars.org/cadr

This implementation uses a Kleene-closure around the alphabet {a, d}
to generate the names; and a macro to define them:

https://github.com/klutometis/cadr/blob/master/src/cadr/core.clj

Tassilo Horn

unread,
Dec 1, 2011, 2:27:22 AM12/1/11
to clo...@googlegroups.com
Peter Danenberg <pe...@factual.com> writes:

Hi Peter,

> Try as I might, I can't purge these anachronisms; so here are `car' to
> `cddddr' in all their glory:
>
> http://clojars.org/cadr

Nice. :-)

> This implementation uses a Kleene-closure around the alphabet {a, d}
> to generate the names; and a macro to define them:
>
> https://github.com/klutometis/cadr/blob/master/src/cadr/core.clj

Is that inspired by the `cxr' macro (I think, that was its name) in Let
Over Lambda? One nice feature that seems missing from your version is a
macro for defining local functions on demand. I don't remember the
exact LoL syntax, but probably it would look something like that:

(with-cxrs [foo caaaddddddaddr,
bar caddddddddaaaaaaaar]
(cons (foo myseq) (bar mysec)))

Bye,
Tassilo

Alan Malloy

unread,
Dec 1, 2011, 5:12:53 AM12/1/11
to Clojure
LoL lets you write:

(with-cxrs
(blah (foo (bar (cadddar x)))))

ie, it looks in your source, sees what you need defined, and makes a
letfn.

This looked fun, so I banged out an implementation:

(defn cxr-impl [name]
(when-let [op (second (re-matches #"c([ad]+)r" name))]
`(comp ~@(map {\a `first \d `rest} op))))

(defmacro with-cxrs [& body]
(let [symbols (remove coll? (tree-seq coll? seq body))]
`(let [~@(for [sym symbols
:let [impl (cxr-impl (name sym))]
:when impl
thing [sym impl]]
thing)]
~@body)))

user> (macroexpand-1 '(with-cxrs (inc (caadaaddadr x))))
(let [caadaaddadr (comp first first rest first first rest rest first
rest)]
(inc (caadaaddadr x)))

Alan Malloy

unread,
Dec 1, 2011, 5:16:10 AM12/1/11
to Clojure
(btw I threw this together just before bed, so it's not perfect. One
thing that's wrong is it assumes (name x) can be called on any x; but
it can't be called on lots of things, like numbers. So you'd need to
filter those out, and also fix a couple other bugs.

Tassilo Horn

unread,
Dec 1, 2011, 7:34:02 AM12/1/11
to clo...@googlegroups.com
Alan Malloy <al...@malloys.org> writes:

Hi Alan,

> LoL lets you write:
>
> (with-cxrs
> (blah (foo (bar (cadddar x)))))
>
> ie, it looks in your source, sees what you need defined, and makes a
> letfn.

Ah, even better. Or well, not better, if you have too many x-es. There
I'd prefer to give shorter names to my accessors.

Anyway, nice bed-time hack!

Bye,
Tassilo

Peter Danenberg

unread,
Dec 1, 2011, 10:53:30 AM12/1/11
to clo...@googlegroups.com
This is fantastic, Alan; I haven't gotten around to LoL yet, but maybe
I should.

Quoth Alan Malloy on Setting Orange, the 43rd of The Aftermath:

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

Reply all
Reply to author
Forward
0 new messages