On Tue, Jul 2, 2013 at 4:45 AM, Islon Scherer <islonsche...@gmail.com> wrote: > One things that always bugged me about clojure is that most functions that > work on collections return seqs and not the original data structure type: > > (= [2 4 6] (-> [2 [4]] flatten (conj 6))) > => false > > Every time I transform collections I need to be aware of this. > My questions is: is there a philosophical reason for that or it was > implement this way because it's easier? Why conj is different? > Doesn't it break the principle of least surprise?
This prompted me to do a little REPL exploration as it surprised me in principle. My understanding of the sequence abstraction was that it was an abstraction over concrete types like list, vector, hash-sets, hash-maps, etc. The fact that many of the operations that work on those concrete types through the sequence abstraction returned seqs didn't matter because their concrete types were stilled preserved under the hood. So I had the following REPL session: user> ; let's conj onto a vector, list, and hash-set user> (conj [:a] :b) [:a :b] user> (conj (list :a) :b) (:b :a) user> (conj #{:a} :b) #{:a :b} user> (conj #{:a} :a) #{:a} user> ; notice that semantics of the hash-set are preserved and a duplicate value is impossible to conj on user> ; now let's conj onto a seq of a vector, list, and hash-set user> (conj (seq [:a]) :b) (:b :a) user> (conj (seq (list :a)) :b) (:b :a) user> (conj (seq #{:a}) :b) (:b :a) user> (conj (seq #{:a}) :a) (:a :a) user> ; notice how the semantics of the underlying datastructure are destroyed in all cases but the list, given that vectors are best conjed onto at the back, and hash-sets should not allow duplicates. user> ; this violates my understanding of seqs, in that I thought they were an abstraction over concrete types which conj would always honor despite the fact that they were an instance of an abstraction. Anyway, I'm sure I'm lightyears behind everyone else but I feel like I remember Rich at one point saying that you should never fear to use conj or worry about seqs because the underlying types will always be preserved and honored. Anyway, blew my mind. :) -- In Christ, Timmy V. http://blog.twonegatives.com/ http://five.sentenc.es/ -- Spend less time on mail -- -- 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.