I wrote this macro a while ago that allows you to do this in a  
relatively clean way:

http://paste.lisp.org/display/81703

Example usage:

=> (let-map [a 5 b (+ a 12)])
{:a 5, :b 17}

- Chas

On Dec 9, 2009, at 1:20 PM, bOR_ wrote:

> Hi all,
>
> I want to make a hash-map where the value of one key depends on the
> values of other keys in the hash-map. Is there a way to do this,
> without needing an external reference to the hash-map?
>
> {:a 1 :b 2 :c #(+ :a :b)}
>
> Similarly, when filling a struct, I often want to refer to the bits I
> already have filled in. I solve that now by just embedding the (struct
> mything.. in a let, and just use the serial nature of let to calculate
> for example c from a and b. Is there a way that while filling a struct
> I can refer to it?
>
> I would like to do this
>
> (defstruct virus :epitopes :mutations :viral_load)
> (def myvirus (struct virus 3 5 (+ (* 0.1 :epitopes) (*
> 0.2 :mutations))))
>
> Instead of
>
> (defstruct virus :epitopes :mutations :viral_load)
> (let [epitopes 3
>        mutations 5
>        viral_load (+ (* 0.1 epitopes) (* 0.2 mutations))]
> (def myvirus (struct virus epitopes mutations viral_load))
>
> As you can see there is some room for being more concise and compact
> if you can refer to keys in the same hash-map or struct, even if the
> hash-map / struct is still being constructed.
>
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to