[racket] Object% equal? for Hash#

From: Ted (tedr56 at gmail.com)
Date: Fri Nov 2 10:12:01 EDT 2012


> > But, now I have to had an attribute wich do not have to tested and I can't
> > figure the best way out.
> >
> > Would I investigate with inspector or the equal<%> interface.
> > Both ways, I don't know how to do this. The doc is pretty brief about that.



> Are you saying that there are fields in your object that you don't
> want to be part of the equality test?
> 

Yes, that's exactly what I meant.

> If so, it does sound like implementing the equal<%> interface may give
> you enough power to express that idea.
> (http://docs.racket-lang.org/reference/objectequality.html)

And that's exactly what I was struggling to.


> Here's a small example:
> 
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> #lang racket
> 
> (define ci-word%
>   (class* object% (equal<%>)
> 
>     ;; Initialization
>     (init-field word)
>     (super-new)
> 
>     ;; We define equality to ignore case:
>     (define/public (equal-to? other recur)
>       (string-ci=? word (get-field word other)))
> 
> 
>     ;; The hash codes need to be insensitive to casing as well.
>     ;; We'll just downcase the word and get its hash code.
>     (define/public (equal-hash-code-of hash-code)
>       (hash-code (string-downcase word)))
> 
>     (define/public (equal-secondary-hash-code-of hash-code)
>       (hash-code (string-downcase word)))))
> 
> (define h (make-hash))
> (hash-set! h (new ci-word% [word "foobar"]) 'value)
> 
> ;; Of course, we expect this lookup to succeed:
> (hash-ref h (new ci-word% [word "foobar"]))
> 
> ;; but it will also match case-insensitively:
> (hash-ref h (new ci-word% [word "FoOBaR"]))


Thank you very much. I tried all possibilities execept this way.
What I can't figured out is the end of the doc with :
The equal<%> interface is implemented with interface* and prop:equal
+hash.
I tought I had to implement the equal<%> interface myself.

Thanks again for the help.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121102/54850eee/attachment.html>

Posted on the users mailing list.