Created
July 16, 2012 05:46
-
-
Save mavericklou/3120891 to your computer and use it in GitHub Desktop.
my homework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns wordcount.core | |
(:use [clojure.java.io :only [reader]] | |
[clojure.string :only [split]])) | |
(defn- word-count [lines] | |
(reduce #(+ %1 (count (split %2 #" "))) 0 lines)) | |
(defn- tab-delimited? [lines] | |
(loop [rest-lines lines columns 0] | |
(if (first rest-lines) | |
(let [next (first rest-lines) | |
current-columns (count (split next #" "))] | |
(cond | |
;is rest lazy? | |
(or (= 0 columns) (= columns current-columns)) (recur (rest rest-lines) current-columns) | |
(not= columns current-columns) false)) | |
true)) | |
) | |
(defn -main [path & args] | |
(when args | |
(let [first-arg (first args) | |
lines (line-seq (reader path))] | |
(cond | |
(= "-t" first-arg) (tab-delimited? lines) | |
(= "-c" first-arg) (word-count lines))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment