Created
July 12, 2012 00:55
-
-
Save dirtyvagabond/3094864 to your computer and use it in GitHub Desktop.
Quick and dirty tabwc per Clojure Class assignment
This file contains 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
;; | |
;; project.clj | |
;; | |
(defproject tabwc "0.0.0-SNAPSHOT" | |
:description "A Clojure Class Assignment! CLI util to validate a TAB file, or do a wc." | |
:main tabwc.core | |
:dependencies [[org.clojure/clojure "1.4.0"]]) | |
;; | |
;; core.clj | |
;; | |
(ns tabwc.core | |
(:require [clojure.java.io :as io]) | |
(:gen-class)) | |
(defn count-words [s] | |
(count (re-seq #"\w+" s))) | |
(defn wc [file] | |
(count-words (slurp file))) | |
(defn tab-count [line] | |
(count (re-seq #"\t" line))) | |
(defn tab-loop [[line & more-lines] header-cnt] | |
(if (nil? line) | |
"OK" | |
(if (= (tab-count line) header-cnt) | |
(recur more-lines header-cnt) | |
(str "This line is not valid:\n" line " (tab count: " (tab-count line) ")")))) | |
(defn tab-validate [file] | |
(with-open [rdr (io/reader file)] | |
(let [lines (line-seq rdr)] | |
(if-let [fline (first lines)] | |
(let [header-cnt (tab-count fline)] | |
(println "Header tab count:" header-cnt) | |
(tab-loop (rest lines) header-cnt)) | |
(println "File appears empty"))))) | |
(def USAGE | |
"Usage: | |
lein run [FILE] [SWITCH] | |
SWTICH should be either of: | |
--wc runs a word count on file | |
--validate determines whether file is a valid TAB file | |
Example usage: | |
lein run myfile.txt --validate") | |
(defn -main [file & [switch]] | |
(println | |
(condp = switch | |
"--wc" (wc file) | |
"--validate" (tab-validate file) | |
USAGE))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment