Skip to content

Design data structureλ︎

One of the first decisions is how to design the data structure to hold our short url and full url addresses.

The simplest approachλ︎

We could create a really simple data structure with a vector

["goouk" ""]

In order to hold multiple short url mappings, we could have a vector of vectors

[["duckduckgo" ""]
 ["practicalli" ""]
 ["slashdot" ""]]
Although the above is nice and simple, it does not provide any context for the data.

Using a map for contextλ︎

The keys in a map can add specific meaning and context to the design of the data structure.

Here are two examples, the first with keys as strings the second as keys as keywords.

{"short-url" "practicalli" "full-url" ""}

{:short-url "practicalli" :full-url ""}

As keys need to be unique, then if we have multiple short url mappings to contain, then we need another data structure for each map.

[{:short-url "practicalli" :full-url ""}
 {:short-url "slashdot" :full-url ""}]

In the above design we cannot use a single map as all the keys in a map need to be unique

Simplifying the mapλ︎

As keys must be unique in a map, we cannot have multiple keys called :short-url, therefore using that design we cant have a single map.

We could simplify the map and remove the current keys and use the value for the short-url as the key and the full url as the value. This means we could just have a single map for all our short-url mappings.

{"practicalli" ""
 "slashdot"    ""
 "duckduckgo"  ""}

Using Clojure keywords for the keys would also allow us to look up the full url addresses using the feature of maps that make keywords act like functions. This feature of the keyword in a map is just like calling the get function on the map with a specific key.

(def url-map
  {:practicalli ""
   :slashdot ""
   :duck-duck-go ""})

(get url-map :practicalli)
;; => ""

(url-map :practicalli)
;; => ""

(:practicalli url-map )
;; => ""