Skip to content

Instantly share code, notes, and snippets.

@mavericklou
Created July 16, 2012 05:46
Show Gist options
  • Save mavericklou/3120891 to your computer and use it in GitHub Desktop.
Save mavericklou/3120891 to your computer and use it in GitHub Desktop.
my homework
(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