8. Namespaces, Libraries and Leiningen
Activity 8.01: Altering the Users List in an Application
Solution:
- Import the
clojure.string
namespace withuse
and the:rename
keyword for thereplace
andreverse
functions:(use '[clojure.string :rename {replace str-replace, reverse str-reverse}])
- Create a set of users:
(def users #{"mr_paul smith" "dr_john blake" "miss_katie hudson"})
- Replace the underscore between honorifics and first names:
(map #(str-replace % #"_" " ") users)
This will return the following:
("mr paul smith" "miss katie hudson" "dr john blake")
- Use the
capitalize
function to capitalize each person's initials in the user group:(map #(capitalize %) users)
This will return the following:
("Mr_paul smith" "Miss_katie hudson" "Dr_john blake")
- Update the user list by using the string's
replace
andcapitalize
functions:(def updated-users...