On Aug 27, 10:37 pm, Parth Malwankar <[EMAIL PROTECTED]>
wrote:
> On Aug 28, 12:10 am, Rich Hickey <[EMAIL PROTECTED]> wrote:
>
> > I posted a variant here:
>
> >http://paste.lisp.org/display/65964
>
> Rich,
>
> It works very nicely. Thanks.
>
> Just one thought in case the functions args are still being
> decided on. Could we consider taking access path as a
> vector rather than directly as function args.
>
> Here is the use case I have in mind.
>
> I think most access paths [:a :b :c] would be generated.
> Nested structures would be something like
>
> processor -> GPRs (general purpose regs) -> r0 r1 .. rN
> -> FPRs (floating point regs) -> f0 f1 .. fN
>
> fridge -> fruits -> apple mango ...
> -> veggies -> eggplant ...
> -> diary -> milk yoghurt ...
>
> So the developer may set up something like a
> (fridge-item-path (get-fruit)) => [:fruits :apple]
> (processor-reg-path (get-reg-arg-from-instruction)) => [:gpr-set :r0]
>
> With the current arg handling this is what we would need to do:
>
> user=> (item-path :mango)
> [:fruits :mango]
>
> user=> (apply mk-get my-fridge (conj (item-path :mango) :quantity))
> 30
>
> user=> (apply mk-assoc my-fridge (conj (item-path :mango) :quantity
> 40))
> {:fruits {:mango {:quantity 40, :color :yellow},
> :apple {:quantity 20, :color :red}},
> :diary-products {:milk {:quantity 1, :color :white,
> :type :low-fat},
> :yoghurt {:quantity 10, :color :pink,
> :type :strawberry}}}
>
> [formatting added above for readability]
>
> In case the access path were vectors the above could become:
>
> (mk-get my-fridge (item-path :mango) :quantity)
> (mk-assoc my-fridge (item-path :mango) :quantity new-quantity)
>
> Much less noise.
>
I don't see this (partial path + separate last key) as a general way
of doing things. The only other way I would consider is the entire
path as a sequence:
(defn mk-get [m ks]
(reduce get m ks))
(defn mk-assoc [m [k & ks] v]
(if ks
(assoc m k (mk-assoc (get m k) ks v))
(assoc m k v)))
;usage
(def nx {:a {:b {:c {:content [1 10] :other 2}}}})
(mk-get nx [:a :b :c :content])
-> [1 10]
(mk-assoc nx [:a :b :c :content 0] 42)
-> {:a {:b {:c {:other 2, :content [42 10]}}}}
(mk-assoc {} [:a :b :x] 42)
-> {:a {:b {:x 42}}}
Rich
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---