Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I wrote this without much thought:

(defn- tail-cmd
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (clojure.string/join
   " | "
   (flatten [(format "tail -n %s -f %s" (or n 200) file)
             (map #(format "grep --line-buffered -v \"%s\"" %)
                  ignore-patterns)])))

On further thought, I'm not sure if it can simplified further. Can it be?

(This is my first question at Code Review!)

share|improve this question

1 Answer

You may or may not consider this simpler:

(defn tail-cmd-2
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (str "cat "
       file
       " | "
       (apply str (map #(format "grep -v \"%s\" | " %) ignore-patterns))
       "tail -f "
       (if n (str "-n " n))))

Or if you're willing to use the non-standard clojure.core.strint library, you can do the following.

(defn tail-cmd-5
  "Construct a tail cmd"
  [file n & ignore-patterns]
  (let [greps   (apply str (for [p ignore-patterns] (format "grep -v \"%s\" | " p)))
        minus-n (if n (str "-n " n))]
    (<< "cat ~{file} | ~{greps} tail -f ~{minus-n}")))

Here's the library source: https://github.com/clojure/core.incubator

share|improve this answer
Yes, it is subjective .. but I do like the fact that both join and flatten have been obviated in your code. – Sridhar Ratnakumar Nov 3 '11 at 20:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.