Re: Confused about "=", native java arrays and seqs...
On Wed, Mar 24, 2010 at 4:17 PM, Frank Siebenlist wrote: > Thanks for the response - guess it is the best one can do living in this > mixed mutable/immutable world. > > Has the seq'ed version of the java byte array become immutable or do we have > to pray that nobody changes the underlying array values? Pray. (let [a (into-array (range 5)), s (seq a)] (prn s) (aset a 1 42) (prn s)) ; (0 1 2 3 4) ; (0 42 2 3 4) I'd recommend keeping the exposure of arrays to very small and isolated pieces of code, if they must be used at all. --Chouser http://joyofclojure.com/ -- 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 To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Re: Confused about "=", native java arrays and seqs...
Thanks for the response - guess it is the best one can do living in this mixed mutable/immutable world. Has the seq'ed version of the java byte array become immutable or do we have to pray that nobody changes the underlying array values? -FS. On Mar 24, 2010, at 12:40 PM, André Ferreira wrote: > Arrays are mutable, seqs immutable. Clojure ='s compares immutable > structures by value, and mutable structures by reference (for the > objects that it is aware of, for others it just prays that they have a > resoanable equals method). This behaviour is described in the very > interisting Henry Baker's egal Paper, and yields the cleanest equality > semantics that I know of. > > On 23 mar, 14:24, Frank Siebenlist wrote: >> My REPL shows: >> ... >> user> (= (bytes (.getBytes "a"))(bytes (.getBytes "a"))) >> false >> user> (= (seq (bytes (.getBytes "a"))) (seq (bytes (.getBytes "a" >> true >> ... >> >> in other words, "equality" for java byte arrays is defined differently than >> their seq'ed version. >> >> It seems that the native arrays are compared on their reference while the >> seq'ed version on their value (as it should...). >> >> Sometimes the seq'ing seems implied, however, like in: >> >> ... >> user> (first (bytes (.getBytes "a"))) >> 97 >> ... >> >> but for the "=" function it is not. >> >> This doesn't feel right and is confusing to say the least. >> >> Could anyone shed some light on this behaviour? >> >> Thanks, Frank. > > -- > 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 > > To unsubscribe from this group, send email to > clojure+unsubscribegooglegroups.com or reply to this email with the words > "REMOVE ME" as the subject. -- 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 To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Re: Confused about "=", native java arrays and seqs...
Arrays are mutable, seqs immutable. Clojure ='s compares immutable structures by value, and mutable structures by reference (for the objects that it is aware of, for others it just prays that they have a resoanable equals method). This behaviour is described in the very interisting Henry Baker's egal Paper, and yields the cleanest equality semantics that I know of. On 23 mar, 14:24, Frank Siebenlist wrote: > My REPL shows: > ... > user> (= (bytes (.getBytes "a"))(bytes (.getBytes "a"))) > false > user> (= (seq (bytes (.getBytes "a"))) (seq (bytes (.getBytes "a" > true > ... > > in other words, "equality" for java byte arrays is defined differently than > their seq'ed version. > > It seems that the native arrays are compared on their reference while the > seq'ed version on their value (as it should...). > > Sometimes the seq'ing seems implied, however, like in: > > ... > user> (first (bytes (.getBytes "a"))) > 97 > ... > > but for the "=" function it is not. > > This doesn't feel right and is confusing to say the least. > > Could anyone shed some light on this behaviour? > > Thanks, Frank. -- 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 To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.
Re: Confused about "=", native java arrays and seqs...
Sorry, I copied the wrong snippet from my repl - the correct one is: ... user> (= (.getBytes "a")(.getBytes "a")) false user> (= (seq (.getBytes "a")) (seq (.getBytes "a"))) true ... although the previous extra "(bytes...)" doesn't change the example, it may add to the confusion ;-) -FS/ On Mar 23, 2010, at 10:24 AM, Frank Siebenlist wrote: > My REPL shows: > ... > user> (= (bytes (.getBytes "a"))(bytes (.getBytes "a"))) > false > user> (= (seq (bytes (.getBytes "a"))) (seq (bytes (.getBytes "a" > true > ... > > in other words, "equality" for java byte arrays is defined differently than > their seq'ed version. > > It seems that the native arrays are compared on their reference while the > seq'ed version on their value (as it should...). > > Sometimes the seq'ing seems implied, however, like in: > > ... > user> (first (bytes (.getBytes "a"))) > 97 > ... > > but for the "=" function it is not. > > This doesn't feel right and is confusing to say the least. > > Could anyone shed some light on this behaviour? > > Thanks, Frank. > -- 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 To unsubscribe from this group, send email to clojure+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.