If you say so. I mean, I see that your example produces output
different than I might expect, but that's a function of clojure's
basic API - memoize will do the same thing:

user> (def q (atom 1))
#'user/q
user> (defn myinc [ignored] (swap! q inc))
#'user/myinc
user> (def m (memoize myinc))
#'user/m
user> (m 1)
2
user> (m 1)
2
user> (m 2)
3
user> (m '(:a))
4
user> (m '(:a))
4
user> (m [:a])
4

Obviously you shouldn't use memoize for a function like myinc, which
isn't referentially transparent; I'm just using it to demonstrate
whether memoize returns a saved result or calls myinc.

If clojure says that [:a] and '(:a) are equal then you can't have them
as two separate keys in a hash; I don't see the problem with this. If
you want to do this with two arguments that are equal, you can use a
vector instead: (map vector x (map class x)). Either way it's a
convenient wheel that has probably been invented too many times.

On Aug 16, 3:05 pm, wwmorgan <wmorga...@gmail.com> wrote:
> Sorry for the double-post. group-by actually maps outputs to inputs.
> But you can run into trouble even with referentially transparent
> functions, because of Clojure's equality semantics:
>
> user=> (def x [(list :a) (vector :a)])
> #'user/x
> user=> (zipmap x (map class x))
> {(:a) clojure.lang.PersistentVector}
>
> - Will Morgan
>
> On Aug 16, 5:52 pm, wwmorgan <wmorga...@gmail.com> wrote:
>
> > Note that if f is not referentially transparent, you can get different
> > results depending on the order in which the collection is traversed,
> > for unordered collections. See the API function group-by. It makes
> > this behavior explicit by mapping each input argument to a vector of
> > output arguments, in the same order as their corresponding inputs were
> > traversed.
>
> > - Will Morgan
>
> > On Aug 16, 3:31 am, Alan <a...@malloys.org> wrote:
>
> > > (defn apply-keys [f ks]
> > >   (zipmap ks (map f ks)))
>
> > > Trivial to write, but it can be quite useful. For example:
> > > (defn whatever [arg]
> > >   (let [some-list (make-list-from arg)
> > >         mapped (map myfunc some-list)]
> > >     (zipmap some-list mapped)))
>
> > > compared to
>
> > > (defn whatever [arg]
> > >   (apply-keys myfunc (make-list-from arg)))
>
> > > I was working on my project and found myself doing this fairly often;
> > > I searched high and low in clojure.core and clojure.contrib but
> > > couldn't find anything similar. This amazed me, because it seems so
> > > common and generic that I assumed clojure would do it for me. If this
> > > isn't already in a standard library, I'd like to see it added. I'm
> > > happy to give it away to someone who maintains a package that would
> > > "fit" this function, or if someone wants to explain to me how to add
> > > my own package I'm willing to do that.
>
> > > Does this seem useful to anyone else?
>
>

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