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

[["goouk" ""]
 ["jrock" ""]
 ["slash" ""]]

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" "jrock" "full-url" ""}

{:short-url "jrock" :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 "jrock" :full-url ""}
 {:short-url "slash" :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.

{"jrock" ""
 "slash" ""
 "goouk" ""}

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 
  {:jrock ""
   :slash ""
   :goouk ""})

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

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

results matching ""

    No results matching ""