FAQ
Hi everyone,
I had a function that reads the contents of a file (in this case, it
represents a license) and then verifies the contents.

As I started to expand the code for verifying, it made sense to break the
function up into a function for file parsing and a function for
verification. The following is the function that I created to return the
contents of the file parsing:

(defn- lic-file-msg-sigs
   "return a vector containing the original license string/message and a seq
of each signature line generated. the input is the string of the entire
license file"
   [lic-file-str]
   (let [result (promise)]
     (with-open [rdr (BufferedReader. (StringReader. lic-file-str))]
       (let [lines (line-seq rdr)
             line-sandwich-middle-fn (fn [lines line-before line-after]
                                          (->> lines
                                               (drop-while #(not (re-find
(re-pattern line-before) %)))
                                               rest
                                               (take-while #(not (re-find
(re-pattern line-after) %)))))
             msg-lines (line-sandwich-middle-fn lines LICENSE-BEGIN
LICENSE-END)
             sig-strs (line-sandwich-middle-fn lines SIGNATURE-BEGIN
SIGNATURE-END)
             msg (clojure.string/join \newline msg-lines)]
         (str msg sig-strs) ;; need to realize the values to force file
         ;; parsing before file is closed. couldn't figure out how to
         ;; force realization except for the str function
         (deliver result [msg sig-strs])))
     @result))


My question is related to the comments towards the end -- is there a better
way to force the realization of the contents of the line-seq before I
deliver it?

(If there is a better way to write this code, let me know.... In case you
are wondering, I wanted to use line-seq for parsing the file so that I get
the contents of the lines in between special sentinel lines. line-seq
requires a reader, and readers are best used with with-open. But with-open
closes the reader object at the end of its scope, so any code using the
contents of the reader need to be realized before the reader is closed. In
order to return the contents outside of the with-open form, I used a
promise & deliver.)

Thanks,
Elango

--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 6 | next ›
Discussion Overview
groupclojure @
categoriesclojure
postedMay 28, '13 at 12:32a
activeMay 28, '13 at 6:32a
posts6
users3
websiteclojure.org
irc#clojure

People

Translate

site design / logo © 2021 Grokbase