It's not terse, but it is easier to follow.
(defn flip
[f x y]
(f y x))
(defn update-nums
[m f]
(update m :qs
(fn [x]
(flip map x
(fn [x]
(update-in x [:nums]
#(map f %)))))))
;; (update-nums m inc)
;;=> {:qs ({:nums (4 2 3)} {:nums (8 5)})}
On Tuesday, October 10, 2017 at 12:27:45 PM UTC-4, Rob Nikander wrote:
>
> Gah. I changed one name and not another. I meant:
>
> (update-in m [:qs * :nums] ...)
>
>
>
> On Tuesday, October 10, 2017 at 10:18:56 AM UTC-4, Rob Nikander wrote:
>>
>> Hi,
>>
>> Say I have a map like this:
>>
>> (def m {:qs [{:nums [3 1 2]} {:nums [7 4]}]})
>>
>> I want to transform each number with a function (say, `inc`). I imagine
>> something like this:
>>
>> (update-in m [:qs * :cell-fns] #(map inc %))
>> ; or
>> (update-in m [:qs * :cell-fns *] inc)
>>
>>
>> But of course you can't write that. The following code works, but I
>> don't like reading it:
>>
>> (update m :qs
>> (fn [qs]
>> (mapv
>> (fn [q]
>> (update q :nums #(mapv inc %)))
>> qs)))
>> => {:qs [{:nums [4 2 3]} {:nums [8 5]}]}
>>
>> Is there an idiomatic way to do this, that looks more like the shorter
>> `update-in` idea?
>>
>> Rob
>>
>>
>>
>>
--
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
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.