Destructuring
Destructuring allows you to remove data elements from their structure or disassemble a structure. It is a technique that improves the readability and conciseness of your code by providing a better tool for a widely used pattern. There are two main ways of destructuring data: sequentially (with vectors) and associatively (with maps).
Imagine that we need to write a function that prints a formatted string given a tuple of coordinates, for example, the tuple [48.9615, 2.4372]
. We could write the following function:
(defn print-coords [coords] (let [lat (first coords) lon (last coords)] (println (str "Latitude: " lat " - " "Longitude: " lon))))
This print-coords
function takes a tuple of coordinates as a parameter and prints out the coordinates to the console in a nicely formatted string, for example, Latitude:
48.9615
– Longitude:
2...