Double-posting myself, here, just to join the fun. You can generalize
if you don't always want the same behavior:

user> (defn apply-by [glue]
        (fn [f keys]
            (glue keys (map f keys))))
#'user/apply-by

user> ((apply-by zipmap) inc (range 5))
{4 5, 3 4, 2 3, 1 2, 0 1}

user> ((apply-by (partial map vector)) inc (range 5))
([0 1] [1 2] [2 3] [3 4] [4 5])

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