GithubHelp home page GithubHelp logo

Question/suggestion for hash-table page [from sourceforge / feature requests] How to use CLOS objects as hash-table keys? about cl-cookbook HOT 3 CLOSED

chuchana avatar chuchana commented on August 18, 2024 2
Question/suggestion for hash-table page [from sourceforge / feature requests] How to use CLOS objects as hash-table keys?

from cl-cookbook.

Comments (3)

pnathan avatar pnathan commented on August 18, 2024

CDR-2 covers it. genhash is an available library for this.

from cl-cookbook.

vindarel avatar vindarel commented on August 18, 2024

https://www.reddit.com/r/Common_Lisp/comments/es7wso/the_best_way_to_use_clos_objects_as_keys_for/

Recently I've (re-)discovered that you cannot (successfully) use CLOS objects as keys for hash-tables.
This is due to the fact that hash-tables only allow standard comparators (eq/eql/equal(p)). These do not work for CLOS objects and you cannot provide your own comparator. There is a genhash library that I am going to use now, but I don't like using external libraries for such simple problems.

i recently had a pleasant experience with generic-cl. i didn't want to use the whole thing (or rather, i saw some weird errors when i passed (:mix :some-package-that-shadows-a-cl-symbol :generic-cl) to uiop:define-package and got scared off), so i just did (:shadowing-import-from :generic-cl :equalp :hash-map :make-hash-map :get :hash). then i defined equalp and hash methods. for a class named -> with slots input and output, i have:

(defmethod equalp ((lhs ->) (rhs ->))
  (and (equalp (->-input lhs) (->-input rhs))
       (equalp (->-output lhs) (->-output rhs))))

(defmethod hash ((obj ->))
  (logand (+ (hash (->-input obj))
             (hash (->-output obj)))
          most-positive-fixnum))

from there, i just find & replace'd hash-table => hash-map and gethash => get.

from cl-cookbook.

vindarel avatar vindarel commented on August 18, 2024

But it's too uncommon isn't it?

from cl-cookbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.