It would probably be better to convert to/from normal Clojure maps at the
edges of the Clojure code. If the input is '((k1 v1) (k2 v2) (k3 v3) (k1
v4)) and we want the earliest occurrence of k1 to "win", then that suggests
(into {} (reverse alist)). If the input's flattened that would be (into {}
(reverse (partition 2 alist))). The reverse makes sure that into assoces
{k1 v4} first and then overwrites it with {k1 v1}, so the earliest
occurrence of k1 "wins". If conj onto a map rejects two-element lists (as
opposed to vectors; I don't have the docs or a repl in front of me where I
am right now) then you'll need a (map vec) in there or something as well.

Output is simpler; (seq amap) alone might suffice, but (map seq amap)
should give you the '((k1 v1) (k2 v2) ...) form from prn, and (mapcat seq
amap) '(k1 v1 k2 v2 ...).

Of course, if you want the key-shadowing behavior not as a cheap overwrite
but as a kind of "stack", so that (drop 1) on the original example would
result in (k1 v4) being a mapping, then you need something beyond a normal
Clojure map -- possibly a map of keys to stacks of values (maintained in
vectors, say), or a stack of maps, depending on the exact use case.


On Mon, Nov 18, 2013 at 10:54 AM, Tassilo Horn <t...@gnu.org> wrote:

> Justin Smith <noisesm...@gmail.com> writes:
>
> Hi Justin & Hans-Peter,
>
> > Typically in clojure we use hash-maps (represented literally as {})
> > where other lisps would use an alist.
>
> One difference between alists and maps is that in alists a "key" can
> occur multiple times, and then the first entry with that key shadows all
> following entries with that key (with respect to retrieval with
> `assoc`).
>
> Well, that feature is probably not used very often, but if you can't
> rule out its usage, you also can't convert alists to maps and expect
> they're equivalent.
>
> > Regarding reading assoc list literals, I wouldn't be surprised if
> > someone had written this function already, but I doubt it is in the
> > core language.
>
> This should do the trick:
>
>   (defn alist-assoc [key alist]
>     (first (filter #(= key (first %)) alist)))
>
> And to add a list to an alist one would use `cons` in Clojure just like
> one would use `cons` in CL, Scheme, or Elisp, too.
>
> Bye,
> Tassilo
>
> --
> --
> 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
> ---
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
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
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to