Created
April 27, 2010 10:51
-
-
Save stilkov/380613 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 sample.grep | |
"A simple complete Clojure program." | |
(:use [clojure.contrib.io :only [read-lines]]) | |
(:gen-class)) | |
(defn numbered-lines [lines] | |
(map vector (iterate inc 0) lines)) | |
(defn grep-in-file [pattern file] | |
{file (filter #(re-find pattern (second %)) (numbered-lines (read-lines file)))}) | |
(defn grep-in-files [pattern files] | |
(apply merge (map #(grep-in-file pattern %) files))) | |
(defn print-matches [matches] | |
(doseq [[fname submatches] matches, [line-no, match] submatches] | |
(println (str fname ":" line-no ":" match)))) | |
(defn -main [pattern & files] | |
(if (or (nil? pattern) (empty? files)) | |
(println "Usage: grep <pattern> <file...>") | |
(do | |
(println (format "grep started with pattern %s and file(s) %s" pattern (apply str (interpose ", " files)))) | |
(print-matches (grep-in-files (re-pattern pattern) files)) | |
(println "Done.")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment