On Dec 9, 10:20 am, bOR_ <boris.sch...@gmail.com> 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.

Do you expect that modifying the values of :epitopes or :mutations
should be reflected in the value of :viral_load? If not, then you
could just write a custom function that creates virus instances
instead of using struct, e.g.:

(defn viral-load [epitopes mutations]
  (+ (* 0.1 epitopes) (* 0.2 mutations)))

(defn make-virus [epitopes mutations]
  (struct virus epitopes mutations (viral-load epitopes mutations)))

If you *do* want :viral_load to reflect changes in the other values,
then I'd say you shouldn't include it in the struct.  Actually, either
way this seems to be a caching mechanism which is both orthogonal to
the domain, and tends to be more trouble than it's worth in the long
run.  In short, smells like OO instead of FP, but I could be wrong.



-- 
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