The Contrib library algo.generic provides a function fmap which does
preserve the type of its input.

As for the idea that clojure.core/map should preserve type, it's worth
remembering that in order to map a function over a set and return a
set, we must do two things: (1) apply the function to each element of
the input set, (2) produce a set containing the resulting values. In
general, there is no better way to do this than to pour (map f s) into
a new set, because the shape of the output is for the most part
undetermined by the input (except if the input is empty or is a
*sorted* set of size 1; actually in the latter case there is the
comparator issue, see below).

So, the fundamental operations here are (1) mapping a function over a
sequence / iterator, (2) pouring a sequence of items into a
collection. As it happens, both (1) and (2) are useful in many
different scenarios, separately and together. Thus, they make great
primitives, and Clojure chooses to expose them as such.

Then of course map and filter are lazy and can be used to do things
like (->> some-sorted-set (filter p?) (map f) (take n)); take could be
reordered with map, but not with filter, and if it's ok for filter to
convert to seq implicitly, then it is also ok for map.

In the case of sorted sets and maps, there is no guarantee that the
input collection's comparator can deal with outputs from the function
passed to map.

So, these are some of the available conceptual arguments. There is
also a rather convincing practical argument in the form of the
existing body of Clojure code, written using the existing Clojure core
library and its conventions and achieving, in many cases, amazing
levels of clarity and concision.

Cheers,
Michał


On 9 February 2014 06:07, Mars0i <marsh...@logical.net> wrote:
> Maybe physical identity is too strong of a requirement for equality.  So
> another way to think about it is that it's 'hash-set', 'set', and '#{}' that
> are--you know--"broken", but that there's a fix, which is to always use
> 'sorted-set'.  (I'm assuming that calling 'seq' on any two sorted sets that
> are = always returns seqs that are =.)
>
> (Don't take offense at the tone of my remarks.  I think we're all on the
> same side here.)
>
>
> On Saturday, February 8, 2014 10:14:37 PM UTC-6, Mars0i wrote:
>>
>> Maybe another way to put it is that what is, uh, "broken" isn't 'map' or
>> 'seq', but '=', which is willing to tell you that two things (sets) are the
>> same when they're not!  We also have the non-broken predicate 'identical?',
>> however, that gets it right.  It's nice to also have a set-equal predicate,
>> which ignores differences in how sets are stored, and ... that's what '='
>> is!  However, if we interpret '=' as implying that when the same function is
>> applied to things that are "equal" in its sense, then we are making a
>> mistake: '=' doesn't mean that.  According to this reasoning, nothing here
>> is broken, even from an extra-linguistic perspective.  '=' just shouldn't be
>> misunderstood.  (In a language with different design and style goals, it
>> might been preferable to define "=" to mean what Clojure means by
>> 'identical?', and use something else--perhaps "equivalent?"--for a predicate
>> analogous to Clojure's '='.)
>
> --
> 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