Write a program that cleans up user-entered phone numbers so that they can be sent SMS messages.
The rules are as follows:
- If the phone number is less than 10 digits assume that it is bad number
- If the phone number is 10 digits assume that it is good
- If the phone number is 11 digits and the first number is 1, trim the 1 and use the last 10 digits
- If the phone number is 11 digits and the first number is not 1, then it is a bad number
- If the phone number is more than 11 digits assume that it is a bad number
(ns phone-number (:require [clojure.string :as str])) (def non-digits #"\D") (def parts #"^1?(...)(...)(....)$") (def error-val ["000" "000" "0000"]) (defn- digits [n] (str/replace n non-digits "")) (defn- trio-h [n] (if-let [ps (re-find parts n)] (rest ps) error-val)) (defn- trio [n] (-> n digits trio-h)) (defn number [n] (-> n trio str/join)) (defn area-code [n] (-> n trio first)) (defn pretty-print [n] (let [[area exch line] (trio n)] (format "(%s) %s-%s" area exch line)))
pretty-printis the use of
letnecessary? Or is there a way in Clojure to splice in the result of
(trio n)in-place for the call to
format? The result of
(trio n)is a vector containing three strings and the
formatcall is expecting three
%sstrings to stick into the format string. If there is an alternative to my use of
let, which is preferred?
- Are the names ok? Do they read like Clojure names? Is there a convention for naming a one-off helper function?
- Is it better to keep
trio-ha separate helper function, or
letit inside the using function, or would you just use the code in-place instead?
clojure.string/replacetriggers a warning (since it collides with
clojure.core/replace) so I
:refered all of
clojure.string. Is there a way to
asto avoid the collision?
- Do you have other improvements you could suggest?