Last active
August 29, 2015 14:01
-
-
Save augustl/e859179caf52d7bb07a4 to your computer and use it in GitHub Desktop.
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
(ns clojure-workshop-flatmap.augustl-cat) | |
(defn format-line-with-number [line n] | |
(format "%6d %s" n line)) | |
(defn line-format-noblank-numbered [line n] | |
(if (clojure.string/blank? line) | |
[line n] | |
[(format-line-with-number line n) (inc n)])) | |
(defn line-format-numbered [line n] | |
[(format-line-with-number line n) (inc n)]) | |
(defn line-format-identity [line n] | |
[line n]) | |
(defn format-lines-iter | |
[lines formatter n] | |
(let [line (first lines)] | |
(when (not (empty? lines)) | |
(let [[formatted new-n] (formatter line n)] | |
(cons formatted (lazy-seq (format-lines-iter (rest lines) formatter new-n))))))) | |
(defn format-lines | |
[lines formatter] | |
(format-lines-iter lines formatter 1)) | |
(defn format-files [files formatter] | |
(->> files | |
(map #(line-seq (clojure.java.io/reader % :encoding "UTF-8"))) | |
(map #(format-lines % formatter)))) | |
(defn get-files | |
[files number-noblank-lines? number-lines?] | |
(format-files | |
files | |
(cond | |
number-noblank-lines? line-format-noblank-numbered | |
number-lines? line-format-numbered | |
:else line-format-identity))) |
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
(ns clojure-workshop-flatmap.cli | |
(:gen-class) | |
(:import [org.apache.commons.cli PosixParser Options Option MissingArgumentException HelpFormatter CommandLine] | |
[java.io File]) | |
(:require [clojure-workshop-flatmap.augustl-cat :as cat] | |
[clojure.tools.cli :as cli])) | |
(def options | |
(doto (Options.) | |
(.addOption (Option. "n", false, "Number all output lines")) | |
(.addOption (Option. "b", false, "number nonempty output lines, overrides -n")) | |
(.addOption (Option. "h", "help", false, "Print help for this command")))) | |
(defn print-help [^CommandLine command-line] | |
(println "Concatenates files to standard output") | |
(println) | |
(.printHelp (HelpFormatter.) "cat [OPTION]... [FILE]..." options)) | |
(defn str-to-int [str] | |
(if (nil? str) | |
str | |
(Integer/parseInt str 10))) | |
(defn run-command [^CommandLine command-line] | |
(when (.hasOption command-line "help") | |
(print-help command-line) | |
(System/exit 0)) | |
(when (empty? (.getArgList command-line)) | |
(print-help command-line) | |
(System/exit 1)) | |
(doseq [file (.getArgList command-line)] | |
(when (not (.isFile (File. file))) | |
(println "File not found:" file) | |
(System/exit 1))) | |
(doseq [file (cat/get-files | |
(map #(File. %) (.getArgList command-line)) | |
(.hasOption command-line "b") | |
(.hasOption command-line "n"))] | |
(doseq [line file] | |
(println line)))) | |
(defn run [args] | |
(try | |
(run-command | |
(-> (PosixParser.) | |
(.parse options (into-array String args)))) | |
(catch MissingArgumentException e | |
(println (.getMessage e)) | |
(System/exit 1)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment