Centering text in Clojure

less than 1 minute read

Notes

;; centering_text.clj

(def page-size 20)

(defn padding [page-size line-size]
  (/ (- page-size line-size) 2))

(defn spaces [length]
  (let [infinite-space (repeat " ")]
    (clojure.string/join "" (take length infinite-space))))

(defn centered-line [page-size line]
  (let [line-size (count line)
        pad-size (padding page-size line-size)]
    (str (spaces pad-size ) line)))

(println (centered-line page-size "Hello"))
(println (centered-line page-size "Hello World!"))
(println (centered-line page-size "- Karthikeyan A K"))
(println (centered-line page-size "***"))

Updated: