This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,13 @@
;; 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))))))

View file

@ -0,0 +1,4 @@
;; Wrap line base on regular expression
(defn wrap-line [size text]
(re-seq (re-pattern (str ".{0," size "}\\s"))
(clojure.string/replace text #"\n" " ")))

View file

@ -0,0 +1,12 @@
(def text "In olden times when wishing still helped one, there lived
a king whose daughters were all beautiful, but the youngest was so
beautiful that the sun itself, which has seen so much, was astonished
whenever it shone in her face. Close by the king's castle lay a great
dark forest, and under an old lime-tree in the forest was a well, and
when the day was very warm, the king's child went out into the forest
and sat down by the side of the cool fountain, and when she was bored
she took a golden ball, and threw it up on high and caught it, and
this ball was her favorite plaything.")
(doseq [line (wrap-line 72 text)]
(println line))