> > (defn map-when "Like map but discards logically false entries"
> >  [fn & seqs]
> >  (filter identity (apply map fn seqs)))
>
> I'd use map-when.
>
> > (defn iterate-while [f x]
> >  (take-while identity (iterate f x)))
>
> This one too.
>
> It raises a question, though -- how much functionality should a
> function provide to be worth making everyone who reads the code learn
> the new vocabulary?  I've written each of these inline when I've
> needed them.  Are they better as idioms or functions?

I would say they're better as functions, for several reasons.

First, many of these aren't really "new vocabulary", since you can
clearly tell what they do from their name and knowledge of the
existing core functions.  If you know map and you know take-when and
take-while, you already know what map-when and map-while do.

Moreover, I think some of these actually fill in mysterious gaps in
the language.  For and map are essentially two different ways of
saying the same thing; which to use in a given situation seems to be
largely a matter of aesthetics (except when there are multiple
iterates...).  Yet, for supports :when and :while, whereas there are
no map-when and map-while forms.  Conversely, we get mapcat but no
forcat.

Even if you don't buy this, the other advantage of having, e.g., (map-
when ...) rather than treating (filter identity (map ...)) as
idiomatic is that map-when can be implemented more efficiently, so
that it doesn't cons up an extra list; however, the more efficient
form is much uglier and almost certainly won't become an idiom.

So, to summarize, IMO adding utilities like map-when give you
 - More uniformity in the language
 - More concise programs (without sacrificing readibility)
 - Greater efficiency

Finally, I want to mention that some of the idioms these functions
replace are not as easy to read as that for map-when.  For instance, I
find myself needing to use map-map or merge-reduce fairly frequently,
and the (reduce (fn [m [k v]] (assoc m k (f v))) {} m) type idioms
they replace are much more difficult to read than calls to the
utilities IMO.

Cheers,
Jason

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