DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  • Managing Data Residency, the Demo
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  • Managing Data Residency, the Demo
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  1. DZone
  2. Coding
  3. Java
  4. Clojure: Combining Calls To Doseq And Let

Clojure: Combining Calls To Doseq And Let

Jay Fields user avatar by
Jay Fields
·
May. 19, 13 · Interview
Like (0)
Save
Tweet
Share
4.69K Views

Join the DZone community and get the full member experience.

Join For Free

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.





Clojure

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

Opinions expressed by DZone contributors are their own.

Trending

  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
  • Managing Data Residency, the Demo
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: