Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Clojure: Combining Calls To Doseq And Let

DZone's Guide to

Clojure: Combining Calls To Doseq And Let

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

If you've ever looked at the docs for clojure's for macro, then you probably know about the :let, :when, and :while modifiers. What you may not know is that those same modifiers are available in doseq.

I was recently working with some code that had the following form.  

(doseq [[printer {:keys [id sub-ids]}] {(partial println "sub1") {:id :A :sub-ids [11 12 13 14]}
(partial println "sub2") {:id :B :sub-ids [21 22 23 24]}}]
(let [id-str (name id)]
(doseq [sub-id sub-ids]
(printer id-str sub-id))))
 
;;; -output-
;;; sub1 A 11
;;; sub1 A 12
;;; sub1 A 13
;;; sub1 A 14
;;; sub2 B 21
;;; sub2 B 22
;;; sub2 B 23
;;; sub2 B 24

Upon seeing this code, John Hume asked if I preferred it to a single doseq with multiple bindings. He sent over an example that looked similar to the following example.

(doseq [[printer {:keys [id sub-ids]}] {(partial println "sub1") {:id :A :sub-ids [11 12 13 14]}
(partial println "sub2") {:id :B :sub-ids [21 22 23 24]}}
sub-id sub-ids]
(let [id-str (name id)]
(printer id-str sub-id)))

That was actually the first time that I'd seen multiple bindings in a doseq, and my immediate reaction was that I preferred the explicit simplicity of having multiple doseqs. However, I always have a preference for concise code, and I forced myself to starting using multiple bindings instead of multiple doseqs - and, unsurprisingly, I now prefer multiple bindings to multiple doseqs.

You might have noticed that the second version of the code slightly changes what's actually being done. In the original version the 'name' function is called once per 'id', and in the second version the 'name' function is called once per 'sub-id'. Calling name significantly more often isn't likely to have much impact on your program; however, if you were calling a more expensive function this change could have a negative impact. Luckily, (as I previously mentioned) doseq also provides support for :let.

The second example can be evolved to the following code - which also demonstrates that the let is only evaluated once per iteration.

(doseq [[printer {:keys [id sub-ids]}] {(partial println "sub1") {:id :A :sub-ids [11 12 13 14]}
(partial println "sub2") {:id :B :sub-ids [21 22 23 24]}}
:let [id-str (do (println id) (name id))]
sub-id sub-ids]
(printer id-str sub-id))
 
;;; :A
;;; sub1 A 11
;;; sub1 A 12
;;; sub1 A 13
;;; sub1 A 14
;;; :B
;;; sub2 B 21
;;; sub2 B 22
;;; sub2 B 23
;;; sub2 B 24

That's really the final version of the original code, but you can alter it slightly for experimentation purposes if you'd like. Let's assume we have another function we're calling in an additional let and it's expensive, it would be nice if that only occurred when an iteration was going to happen. It turns out, that's exactly what happens.

(doseq [[printer {:keys [id sub-ids]}] {(partial println "sub1") {:id :A :sub-ids [11 12 13 14]}
(partial println "sub2") {:id :B :sub-ids []}}
sub-id sub-ids
:let [sub-id-times-2 (do (println "binding occured") (* sub-id 2))]]
(printer id sub-id))
 
;;; binding occured
;;; sub1 :A 11
;;; binding occured
;;; sub1 :A 12
;;; binding occured
;;; sub1 :A 13
;;; binding occured
;;; sub1 :A 14
;;; (nothing else was printed, no other binding occurred)

Whether you prefer multiple bindings or multiple doseqs, it's probably a good idea to get comfortable reading both.





Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}