Hi!
Am 31.08.2009 um 11:27 schrieb Timo Mihaljov:
> I have some code that looks like this:
>
> (let [radius 20
> diameter (* 2 radius)
> circumference (* pi diameter)]
> {:radius radius
> :diameter diameter
> :circumference circumference})
>
> I would like to simplify it to something like:
>
> {:radius 20
> :diameter (* 2 (% :radius))
> :circumference (* pi (% :diameter))}
>
> where % is the map itself.
You could define your own let-like construct for this:
(defmacro letmap [[m kvs & mkvs] & body]
(if m
`(let [~m {}
~@(mapcat (fn [[k v]] `(~m (assoc ~m ~k ~v))) kvs)]
(letmap ~mkvs ~...@body))
`(do ~...@body)))
Usage:
(letmap [a-map {:a 12
:b (* 2 (:a a-map))}
another-map {:c (:a a-map)
:d (+ (:b a-map) (:c another-map))}]
[a-map, another-map])
-> [{:b 24, :a 12} {:d 36, :c 12}]
Hope this helps.
Kind Regards,
achim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---