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
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.
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.
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
A string can be converted to a keyword, a character cannot
A character can be converted to a string using the str function
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.)
Putting it all together.
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"
[name]
(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