joe

joe

Web Development with Clojure, Third Edition - Why is vec necessary in (vec (db/get-messages))

I’m really enjoying Web Development with Clojure, 3rd edition. Thanks to the authors for writing it. There are so many separate moving parts to web development in Clojure/Script, and while very powerful when understood, this book is really a “must read” for anyone new to the ecosystem.

While reading version B8.0 of the book I noticed something in the code examples that I’m having some difficulty understanding. Why do the later versions of the guestbook app need to call vec on the messages returned from the database?

In the earlier versions (see [1] below) of the app that render the page on the server, there’s no need to call vec:

[1]
Chapter 3, Think in Terms of Aplication Components > Routing Requests
pdf page: 74
book page: 61
File: https://media.pragprog.com/titles/dswdcloj3/code/guestbook/src/clj/guestbook/routes/home.clj

(defn home-page [request]
  (layout/render
   request "home.html" {:messages (db/get-messages)}))

But in later versions (see [2] and [3] below) of the guestbook app, which are using client-side rendering, vec is used:

[2]
Chapter 4, Build the UI with Reagent > Reimplementing the List
pdf page: 100
book page: 88
File: https://media.pragprog.com/titles/dswdcloj3/code/guestbook-reagent/src/clj/guestbook/routes/home.clj

(defn message-list [_]
  (response/ok {:messages (vec (db/get-messages))}))

[3]
Chapter 5, Services > Guestbook Messages
pdf page: 113
book page: 101
File: https://media.pragprog.com/titles/dswdcloj3/code/guestbook-services-1/src/clj/guestbook/messages.clj

(defn message-list []
  {:messages (vec (db/get-messages))})

When I removed the call to vec in [3], I noticed that the messages are displayed in ascending descending order of :id, which causes the more recent messages to show at the top of the page, instead of at the bottom as it does when vec is called.

I’m not clear how vec has this effect? When I try (vec (db/get-messages)) and just (db/get-messages) at the REPL, the messages are in the same order.

Marked As Solved

svmbrown

svmbrown

Author of Web Development with Clojure

Hi there Joe,

Thanks for reading, and thanks for the compliments!

Your observation that the ordering is the same between (db/get-messages) and (vec (db/get-messages)) is correct, and on the right track to figuring this out. If we were to update [1] to use (vec (db/get-messages)) you’d notice no change at all. In fact, if you write a few messages on [3] with the vec call removed (thus seeing them in the wrong order) and then refreshed the page, you’d see that they’d shuffle into the correct order. The fact that the issue only appears when we update our message-list is the key here.

We use vec here because the conj function prepends to lists (the default type from db/get-messages) but appends to vectors. Clojure does this because conj uses the most efficient method of inserting an element into a collection. Lists are implemented with cons cells, and so prepending is O(1) and appending is O(n), so conj prefers prepending. On the other hand, vectors are quite complex, but are optimized for append and lookup, so conj prefers appending. This is a subtle behaviour, but is an important one to know when working with Clojure.

Having our messages as a list was okay in [1], since we were never updating our list. We were always getting it fresh from the database. However, in [2] and [3], we are optimistically updating our message-list on the client. Since we’d like to append messages, we want a vector. We could convert it on the client side, but in practice it is preferable to have one consistent data model across the application. There is no need for us to keep messages as a list, so the most straightforward solution is to convert it as soon as we get it from our database.

Hope that helps. Enjoy the rest of the book!

Also Liked

joe

joe

Hi Scot,

Thank you for taking the time to write such a clear and thorough answer.

It’s much appreciated!

kentbull

kentbull

First of all, Thank you @joe for asking this question. I had the same question, mystified as to why vec was needed at all.

And thank you, @svmbrown for the clear and detailed answer. I learned something today because you took the time to teach it. For that I am grateful. Now I know why I would use vec and why you chose to use it here.

I’m loving this book and this community!

Where Next?

Popular Pragmatic Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
jgchristopher
“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ” Excerpt From: Br...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Sub Categories: