Something like:

(defn skv [str]
  (seq (.split str " = " 2)))

(defn second-bit-not-equal-sign? [str]
  (not (second (skv str))))

(defn extract* [lseq]
  (when lseq
    (lazy-seq
      (let [[f & r] lseq]
        (if (second-bit-not-equal-sign? f) (throw (Exception. "no key here")))
        (let [[p1 p2] (split-with second-bit-not-equal-sign? r)
              [k v] (skv f)
              v (apply str v p1)]
          (cons [k v] (extract* (seq p2))))))))

(defn extract [lseq]
  (extract* (seq (map clojure.string/trim lseq))))

user=> (extract ["key1 = value1 is there" "and continues" "key2 = new
value" "key3 = value3" "takes some more lines" "as well"])
(["key1" "value1 is thereand continues"]
 ["key2" "new value"]
 ["key3" "value3takes some more linesas well"])

If you want a map, (into {} (extract ...)). The obvious input source
is a line-seq obtained somehow. If you want spaces or newlines at the
concatenation sites (e.g. "value3 takes some more lines as well" or
"value3\ntakes some more lines\nas well") in the output, change (apply
str v p1) to (apply str v (interleave (repeat \space) p1)) or (apply
str v (interleave (repeat \newline) p1)).

I suggest you study the code to understand how the Python you posted
was translated into Clojure so you can more readily do it yourself in
the future with other code you're porting. Be aware, too, of the
effects of Clojure's addition of laziness; particularly, that the lazy
sequence output will be backed by the line-seq, which will be backed
by a reader, and if you let the output pass out of (with-open [rdr
(whatever)] ...) before it's fully realized you'll have problems. (If
you package the output into a map inside of the (with-open ...) you
won't have a problem there.) Also, note that once consumption is
started the underlying line-seq will have one additional element
realized beyond the last one in realized elements of the output, since
it has to be peeked at to know when the current maybe-multi-line
record ends and the next begins. If the individual lines are short,
this shouldn't be an issue even for huge input files (though stuffing
the whole thing into a map would be).

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to