Point taken, this actually got me thinking about the probability of a hash
match between strings of English words. I tried a playful experiment- I used
a dictionary of 800,000 English words (containing 2 to 32 characters culled
from the Internet) I got 2802 occurrences of strings producing the same
hash. Of these 2802 occurrences 2223 are words between 2 and 4 characters.
These matches were almost all line noise words. For words of 9 to 12
characters there are only 10 matches. There are no matches for words of 13
characters or more.
Taking this into account, the probability of getting a false positive on two
data structures of the same type (like a map representing a person) that
have matching public fields but a falsely matching private hash becomes very
unlikely.

As for boinking the hash, c'est la vie since boinking anything in the map
would destroy equality anyway.

However, for those who might be concerned, this following version removes
the hash technique and provides a simple eq? function.

(defn set-private [m k x]
  (let [the-meta    (meta m)
new-private (assoc (:private the-meta) k x)]
    (with-meta m (assoc the-meta :private new-private))))

(defn get-private [m k]
  (get (:private (meta m)) k))

(defn eq? [a b]
  (and (= a b)
       (= (:private (meta a))
  (:private (meta b)))))

On Wed, Apr 22, 2009 at 7:20 PM, Victor Rodriguez <vict...@gmail.com> wrote:

>
> On Wed, Apr 22, 2009 at 1:28 PM, David Nolen <dnolen.li...@gmail.com>
> wrote:
> > You're missing the clever hack ;) I'm hashing the private data structure
> and
> > associating it to the map. This is what is used in the equality test. You
> > can verify yourself that it works.
> > my-object ;; -> {:private -1261861093}
> > my-other-object ;; -> {:private -1261861093}
> > Make sense? A few quick tests show that the hash of a data structure will
> > match on any VM.
>
> OK, I see the clever hack now.  This could work, until murphy's law
> kicks in and you get a false positive when two different sets of
> private data produce the same hash... (or someone boinks the hash
> value in the map).
>
> Cheers,
>
> Victor.
>
>
> > On Wed, Apr 22, 2009 at 1:01 PM, Victor Rodriguez <vict...@gmail.com>
> wrote:
> >>
> >> On Tue, Apr 21, 2009 at 12:01 PM, David Nolen <dnolen.li...@gmail.com>
> >> wrote:
> >> > Nice post thanks for putting it together.  My gut feeling is that the
> >> > need
> >> > for information hiding is still overinflated, but... until someone
> >> > builds a
> >> > 200k LOC Clojure program who will know for sure?
> >> > Here's my shot at a solution for private data:
> >> > (defn set-private [m k x]
> >> >   (let [the-meta    (meta m)
> >> > new-private (assoc (:private the-meta) k x)]
> >> >     (with-meta
> >> >      (assoc m :private (hash new-private))
> >> >      (assoc the-meta :private new-private))))
> >> > (defn get-private [m k]
> >> >   (get (:private (meta m)) k))
> >> > (def my-object (set-private {} :first "Bob"))
> >> > (def my-other-object (set-private {} :first "Bob"))
> >> > (get-private my-object :first) ; -> "Bob"
> >> > (= my-object my-other-object) ; -> true
> >> > No secret keys, no other libraries, and I believe this supports
> equality
> >> > just fine.  Since we're using metadata the data travels around easily
> >>
> >> This won't work, since metadata is not used for equality tests, isn't
> >> that right?
> >>
> >> Regards,
> >>
> >> Victor Rodriguez.
> >>
> >> > between operations.
> >> > ---------- Forwarded message ----------
> >> > From: Mark Engelberg <mark.engelb...@gmail.com>
> >> > Date: Tue, Apr 21, 2009 at 6:41 AM
> >> > Subject: Re: Abstract data types in functional languages
> >> > To: clojure@googlegroups.com
> >> >
> >> >
> >> >
> >> > On Mon, Apr 20, 2009 at 11:00 AM, Timo Mihaljov <noid....@gmail.com>
> >> > wrote:
> >> >> Is the concept of Abstract Data Types [1] useful in Clojure?
> >> >>
> >> >> If yes, how would you implement one?
> >> >
> >> > I have composed a lengthy response to this question, and added it to
> my
> >> > blog:
> >> > http://programming-puzzler.blogspot.com/2009/04/adts-in-clojure.html
> >> >
> >> > I look forward to everyone's comments,
> >> >
> >> > Mark
> >> >
> >> >
> >> >
> >> >
> >> > >
> >> >
> >>
> >>
> >
> >
> > >
> >
>
> >
>

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