Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
13
Task/Word-wrap/Clojure/word-wrap-1.clj
Normal file
13
Task/Word-wrap/Clojure/word-wrap-1.clj
Normal 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))))))
|
||||
4
Task/Word-wrap/Clojure/word-wrap-2.clj
Normal file
4
Task/Word-wrap/Clojure/word-wrap-2.clj
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
;; Wrap line base on regular expression
|
||||
(defn wrap-line [size text]
|
||||
(re-seq (re-pattern (str ".{1," size "}\\s|.{1," size "}"))
|
||||
(clojure.string/replace text #"\n" " ")))
|
||||
3
Task/Word-wrap/Clojure/word-wrap-3.clj
Normal file
3
Task/Word-wrap/Clojure/word-wrap-3.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
;; cl-format based version
|
||||
(defn wrap-line [size text]
|
||||
(clojure.pprint/cl-format nil (str "~{~<~%~1," size ":;~A~> ~}") (clojure.string/split text #" ")))
|
||||
12
Task/Word-wrap/Clojure/word-wrap-4.clj
Normal file
12
Task/Word-wrap/Clojure/word-wrap-4.clj
Normal 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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue