src/phone_number.clj

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(ns phone-number
  (:require [clojure.string :as string]))

(def invalid-number "0000000000")

(defn number [digits]
  (let [clean-number (string/replace digits #"[^\d]+" "")
        match (re-matches #"1?(\d{10})" clean-number)]
    (if match (second match) invalid-number)))

(defn area-code [digits]
  (subs (number digits) 0 3))

(defn pretty-print [digits]
  (let [phone (number digits)]
    (format "(%s) %s-%s" (area-code digits) (subs phone 3 6) (subs phone 6))))

Comments

Thanks for letting me know about clojure.core/format... nice! :)

jcarlosteso commented 7 April 2016 at 17:25 UTC

You're not logged in right now. Please login via GitHub to comment