annotate src/reverse_sentence.clj @ 0:307a81e46071 tip

initial committ
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Oct 2011 01:17:49 -0700
parents
children
rev   line source
rlm@0 1 (ns coderloop.reverse-sentence
rlm@0 2 (:refer-clojure :only [])
rlm@0 3 (:require rlm.ns-rlm rlm.light-base))
rlm@0 4 (rlm.ns-rlm/ns-clone rlm.light-base)
rlm@0 5
rlm@0 6
rlm@0 7 (undef partition)
rlm@0 8 (use '[clojure.contrib [string :only [partition]]])
rlm@0 9 (use '[clojure [string :only [blank?]]])
rlm@0 10
rlm@0 11 (defn reverse-line [s]
rlm@0 12 (apply str
rlm@0 13 (flatten
rlm@0 14 (map #(if (re-matches #"\w" (str (first %))) (reverse %) %)
rlm@0 15 (filter (comp not (partial = ""))
rlm@0 16 (partition #"\w+" s))))))
rlm@0 17
rlm@0 18
rlm@0 19 (defn reverse-file [file]
rlm@0 20 (doall
rlm@0 21 (map println
rlm@0 22 (map reverse-line
rlm@0 23 (read-lines file)))))
rlm@0 24
rlm@0 25
rlm@0 26 (if (command-line?)
rlm@0 27 (reverse-file (first *command-line-args*)))
rlm@0 28