On Mon, 4 May 2009 16:07:06 +0200
Christopher Taylor <ccmtay...@gmail.com> wrote:

> 
> Hi Nathan,
> 
> On 04.05.2009, at 15:47, Nathan Hawkins wrote:
> 
> >
> > On Mon, 4 May 2009 06:16:14 -0700 (PDT)
> > Drew Raines <aarai...@gmail.com> wrote:
> >>
> >> Whoops, that (seq) is a debugging artifact.  You can remove that:
> >>
> >> (let [test-str "foo=1;bar=2;baz=3"]
> >>  (reduce conj {}
> >>          (map #(apply hash-map (.split % "="))
> >>               (.split test-str ";"))))
> >
> > Ok, my example seems to have misled. You're missing the point a
> > little bit:
> >
> > 1. I was trying to avoid the (reduce conj {} ...), by having the map
> > function do it. Why even build a list that's only going to get
> > thrown away when I want a hash-map at the end?
> 
> you're not actually building a list. The function map returns a
> (lazy) *sequence*, which is an instance of ISeq. This just means
> you're getting something that supports the operations first and rest.
> So, since map returns a sequence and you want a Map (i.e. key/value
> data structure), you'll have to turn it into one by using (conj
> {} ...) or (into {} ...).

There's the source of my misunderstanding. I knew map returned a
sequence, but hadn't quite connected "lazy" to the problem at hand.

I had stumbled on a couple different ways of converting the sequence to
a hash-map (I found (into {} ...), (apply hash-map ...) and (reduce conj
{} ...)), but I was thinking in terms of how I'd solve the problem in
Common Lisp so I thought that running map and then converting the
result to a hash-map was going to iterate the list twice, as well as
cons the results twice.

Thanks for clearing that up.

Nathan

--~--~---------~--~----~------------~-------~--~----~
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
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