May 2018
Beginner to intermediate
290 pages
6h 43m
English
The main way to trip up with Clojure’s vectors and lists is to forget just how immutable they are. For example, if you start with this:
| | (def novels ["Emma" "Coma" "War and Peace"]) |
and then add a new book like this:
| | (conj novels "Jaws") |
you have done nothing. Well, not precisely nothing: you started with the three-element novels vector. Then you created a new vector with four elements. Then you threw that new vector away, leaving the universe pretty much as you found it. To do something useful you need to grab the new, four-element vector, perhaps by binding it to a new symbol:
| | (def more-novels (conj novels "Jaws")) |
Exactly the same logic applies to most other Clojure data structures, including lists: ...
Read now
Unlock full access