On Tue, Oct 21, 2008 at 11:24 AM, Mathias Dahl <[EMAIL PROTECTED]> wrote:
>
> Today I came up with this:
>
> (defn locate-lines [regexp]
> (let [pattern (. java.util.regex.Pattern
> (compile regexp
> (. java.util.regex.Pattern
> CASE_INSENSITIVE)))]
> (debug (str "pattern: " pattern))
> (with-open r (new java.io.LineNumberReader
> (new java.io.FileReader
> "some_large_file.txt"))
> (binding [*in* r]
> (loop [line (read-line) acc nil]
> (if line
> (if (. (. pattern (matcher line)) (find))
> (recur (read-line)
> (if acc
> (cons (list (. r (getLineNumber)) line) acc)
> (list (list (. r (getLineNumber)) line))))
> (recur (read-line) acc))
> (reverse acc)))))))
I don't see any way to significantly improve your speed, but I can
save you some lines of code:
(import '(java.util.regex Pattern)
'(java.io LineNumberReader FileReader))
(defn locate-lines [regexp]
(let [pattern (Pattern/compile regexp Pattern/CASE_INSENSITIVE)]
(debug (str "pattern: " pattern))
(with-open r (LineNumberReader. (FileReader. "some_large_file.txt"))
(binding [*in* r]
(reduce (fn [acc line] (if (.find (re-matcher pattern line))
(conj acc [(.getLineNumber r) line])
acc))
[] (take-while identity (repeatedly read-line)))))))
--Chouser
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---