I have the following code within a let
; clause in a let
lessons-full (into []
(map #(dissoc (merge % (first (lesson-detail %)))
:id :series_id :lesson_id :num))
lessons)
bizarrely I ended up using the transducer form of into by accident, by getting a paren in the wrong place. That's the version shown above. For comparison, version without the transducer is:
; clause in a let
lessons-full (into []
(map #(dissoc (merge % (first (lesson-detail %)))
:id :series_id :lesson_id :num)
lessons))
which also works.
I have a couple more things I want to do in this transform, including convert the value of a key called :type which is currently a string, with keyword. However this is becoming high cognitive load.
Not yet skilled with the threading operators. Can anyone assist with first steps/thinking process for that?
lessons is a list of maps from a jdbc query.
update: draft answer - thinking process to convert to thread last operator
Step 1
Prepare for juggling. 1. Start with thread last ->>, 2. put the argument, lessons up front, 3. put the final transform into at the end. This version works, but we're not finished yet:
; clause in a let
lessons-full (->> lessons
(map #(dissoc (merge % (first (lesson-detail %)))
:id :series_id :lesson_id :num) ,,,)
(into [] ,,,))
Note, the triple commas ,,, are ignored by clojure and we add them to help visualise where the argument is injected by the thread-last ->> macro.
Step 2
We pull out the dissoc but since we used it within a call to map, when we pull it out we need to wrap it in another call to map.
; clause in a let
lessons-full (->> lessons
(map #(merge % (first (lesson-detail %))) ,,,)
(map #(dissoc % :id :series_id :lesson_id :num) ,,,)
(into [] ,,,))
So that works too. Let it sink in.
update 2
Finally, here's code that achieves my original goal:
; clause in a let
lessons-full (->> lessons
(map #(merge % (lesson-detail %)) ,,,)
(map #(dissoc % :id :series_id :lesson_id :num) ,,,)
(map #(assoc % :type (keyword (:type %))) ,,,)
(into [] ,,,))
It appears that list comps are not easy to use inside of the thread last macro, unless I'm mistaken. Also, I'm mapping over the map three times here. In my current use case that might not matter, but is there anything to be said here regarding performance, or any other improvements possible?
compon to your first version?compbut didn't succeed. Would be great if you wanted to post an example :)