5
\$\begingroup\$

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?

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Yes, it is subjective .. but I do like the fact that both join and flatten have been obviated in your code. \$\endgroup\$ – Sridhar Ratnakumar Nov 3 '11 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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