Skip to content

Design a map for name generationλ︎

Imagine you are writing a simple celebrity name generator that takes your name and creates a silly version.

Define a data structure to model celebrity names, containing 3 or more first, second and third names, that has three names for every letter of the alphabet.

Suggested Example
(def celebrity-first-name
  {"a" "Ally-Pally"
   "b" "Bongo"
   "c" "Chipper"})

(def celebrity-second-name
  {"a" "Anstruther"
   "b" "Beaufort"
   "c" "Cholmondeley"})

(def celebrity-third-name
  {"a" "Arbuthnot"
   "b" "Battenburg"
   "c" "Coutts"})

Elaborate on the designλ︎

The following alternative data structure design is very simple and more concise, however it does loose some of the semantic meaning.

The position of the names is not defined in terms of the context of the problem.

(def celebrity-first-names
  {:a ["Ally-Pally" "Anstruther" "Arbuthnot"]})

This next design removes some of the redundancy in defining each letter of the alphabet several times. Apart from less typing and therefore reading by the development team, it also explicitly defines the semantic meaning of each name within the context of this problem.

(def slone-names
  {:a {:first "Ally-Pally" :second "Anstruther" :third "Arbuthnot"}})

The design could be taken further by defining a function that generates a celebrity name.

Creating the algorithm to construct your sloane nameλ︎

  • The first sloane name is chosen from the first character of the first name
  • The second sloane name chosen from the first character of the second name
  • The third sloane name is chosen from the second character of the second name

You can get the first element of a string by treating it just like a collection. However this returns a character

(first "Strings also act as collections")

A string can be converted to a keyword, a character cannot

(keyword "a")

A character can be converted to a string using the str function

(str (first "Strings also act as collections"))

The keywords need to be the same case, so convert the first character to lower case (which returns a string, so the explicit str function is no longer required.)

(clojure.string/lower-case (first "Strings also act as collections"))

Putting it all together.

(keyword (clojure.string/lower-case (first "Strings also act as collections")))

Create a function to calculate your sloane nameλ︎

Putting all this together in a function to generate your sloan name, given your a string with your first and last name.

(defn sloane-name
  "Given a first and last name as a string, returns your equivalent Sloane name as a string"
  (let [first-name  (keyword (clojure.string/lower-case (first (first (clojure.string/split name #" ")))))
        middle-name (keyword (clojure.string/lower-case (first (second (clojure.string/split name #" ")))))
        last-name   (keyword (clojure.string/lower-case (second (second (clojure.string/split name #" ")))))]
    (str (get-in slone-names [first-name  :first])
         " "
         (get-in slone-names [middle-name :second])
         " "
         (get-in slone-names [last-name   :third]))))

Supply a name that will test if the sloane-name function works

(sloane-name "Billy Abstainer")
;; => "Bongo Anstruther Battenburg"