I had just started looking at the object write function again a couple
of days ago. I wanted the convenience of it. I used open-with in a
couple of different places, but didn't get a working generic function out of it.
I didn't spend too much time on it. I realize now that it was laziness
that kept it from working in the function I made.

So how about this one. It is convenient and it encourages efficient and
idiomatic use. And it doesn't suffer from laziness.

It's sort of the opposite of object-seq.

(defn write-objects
  "Write a list of objects to a pail"
  [pail objects]
  (with-open [writer (.openWrite pail)]
    (doall (map #(.writeObject writer %) objects))))

Eric



----- David Cuddeback's Original Message -----
> > +  (let [spec (spec structure)
> > +        pail (apply create spec path (mapcat identity create-key-args))]
> > +    pail))
> > +
> > +(defn find-or-create [structure path]
> > +  "Get a pail from a path, or create one if not found"
> > +  (let [pail (try (pail path)
> > +             (catch Exception e (create-pail structure path)))]
> > +    pail))
> > +
> > +(defn object-write
> > +  "Open pail and write an object to it"
> > +  [pail object]
> > +  (let [os (.openWrite pail)]
> > +    (.writeObject os object)
> > +    (.close os)))
> 
> Clojure's `with-open` is an exception-safe version of the `(let [x ...] ... (.close x))` pattern: http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/with-open
> 
> ---
> Reply to this email directly or view it on GitHub:
> https://github.com/dcuddeback/clj-pail/pull/1/files#r8629093

-- 

Eric Gebhart
e.a.gebhart@gmail.com