RosettaCodeData/Task/Word-wrap/Clojure/word-wrap-1.clj

14 lines
547 B
Clojure

;; Wrap line naive version
(defn wrap-line [size text]
(loop [left size line [] lines []
words (clojure.string/split text #"\s+")]
(if-let [word (first words)]
(let [wlen (count word)
spacing (if (== left size) "" " ")
alen (+ (count spacing) wlen)]
(if (<= alen left)
(recur (- left alen) (conj line spacing word) lines (next words))
(recur (- size wlen) [word] (conj lines (apply str line)) (next words))))
(when (seq line)
(conj lines (apply str line))))))