Over a million developers have joined DZone.

Clojure: Combining Calls To Doseq And Let

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

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.





Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Jay Fields, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}