On Wednesday, March 28, 2012 at 12:58 PM, Timothy Baldridge wrote: > Let's take a step back and look at this idea from a new user's view. > The way I see it, whatever we do, we'll have to explain to users how > to create sets. Hashmaps, and vectors follow (roughly) the JSON/Python > syntax: > > vector = [1 2 3 4] > map = {1 2 3 4} > > So how to these languages represent sets? > > Python does it via extra syntax sugar: > > set = {"foo", "bar"} > map = {"foo": "bar", "foo2" : "bar2"} > > I think the thing we have to keep in mind is that I can't think of a > single other language that has set literals. > >
Good point, but I disagree with the suggestion that because other languages don't have a set literal we shouldn't have one either. I like having the literal, FWIW. It makes sets feel like a more natural datastructure to use throughout the language. I've seen people write all sorts of crazy methods and functions to do what sets give you for free in other languages before they realized "oh right, this should probably be a set". Keeping sets front and center with a literal promotes their use in general. I think that's a Good Thing™. In other languages I sometimes feel like they're an afterthought, and using them feels like they're somehow foreign. As in I often find myself thinking: "Where's my set literal? Why am I not writing this in Clojure?" ;) > So whatever we come up > with will be a bit bizzare to new users. So let's look at the options > given above: > > {{"foo"}} ;; looks like a hash of a hash > <{"foo"}> ;; could work, but no other form in all of clojure has two > forms at the end. > #{"foo"} ;; not exactly pretty > > My thoughts are this...who actually uses the literal hash set? In the > thousands of lines of Clojure code I've written a hash set once. So my > first reaction is why have a literal at all? Why not use: > > That's surprising to me. You never use something like this? (map #{:fred :bob :ted} [:fred :bob]) Would you use: (into (hash-set) [1 2 2 3]) (reduce conj (hash-set) [1 2 2 3]) Instead of: (into #{} [1 2 2 3]) (reduce conj #{} [1 2 2 3]) To my eye, the empty set is a fairly common occurrence and that seems to be how #{} gets used the most. The set literal also seem fairly abundant in test code. Contrived example: (= (map inc #{1 2 3}) '(2 3 4)) This finding is based on a rushed `ack -aui "#{"` of popular projects I'd consider to be "idiomatic" clojure. So take my "research" with a grain of salt. (sorted-set), (sorted-map), and (array-map) make more sense to me to call without args (sorted-set) or with args (sorted-set 1 2 2 3) over introducing a literal. We have: '(), [], {}, #{} No one seems to be dumbfounded by '(). I suppose one could imagine something nasty like $1 2 3$ being proposed for a single character set wrapper, but blech. No thank you. I rather like #{} and wouldn't be shy about introducing it to a beginner. > On top of that, lets say we do want a literal syntax...if we have to > teach some bizarre syntax that people will rarely use...what's the big > difference between #{} and <{}> ? > > If we want to actually go back to the original lisps, they didn't have > any special syntax at all, besides the quote literal. This isn't > ruby/python/C#. If something can be implemented without syntactic > sugar, most times it should be. So my question for Cedric is...why all > the fuss about set? Why not just use (set ["foo", "bar"]) it's clean, > and makes plenty of sense to newbies. > > Timothy > > -- > 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 > (mailto: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 > (mailto: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 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