Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expressions not valid in hash-set creation #2

Open
mx opened this issue Dec 10, 2021 · 3 comments
Open

Expressions not valid in hash-set creation #2

mx opened this issue Dec 10, 2021 · 3 comments

Comments

@mx
Copy link

mx commented Dec 10, 2021

Looks like #{...} can often only accept immediate values. For example:

$ sbcl
* (require :reader)
NIL
* (require :hash-set)
NIL
* (reader:enable-reader-syntax 'get-val 'hash-table 'not 'string 'describe 'array 'set 'run-program)
#<READTABLE {100261DBA3}>
* (defvar x #{(cons 1 2)})

debugger invoked on a TYPE-ERROR @536416CB in thread
#<THREAD "main thread" RUNNING {1004DC81F3}>:
  The value
    2
  is not of type
    LIST

;; ...

* (defvar s #{:a 1 :b 2})
S
@digikar99
Copy link
Owner

While #{...} can handle non-immediate-value expressions, hash set does handle lists in an unexpected manner.

CL-USER> (let ((reader:*set-function* 'hash-set:list-to-hs)) ; hash-set:list-to-hs converts lists to hash-sets
           (hash-set:dohashset (x (eval (read-from-string "#{(list 1 2)}")))
             (princ x)))
#<HASH-SET of count: 2 {1042E1B5C3}>
NIL
CL-USER> (let ((reader:*set-function* 'hash-set:list-to-hs)) ; #{...} can handle non-immediate values
           (hash-set:dohashset (x (eval (read-from-string "#{(+ 2 3)}")))
             (princ x)))
5
NIL

One could write their own function that behaves as expected:

CL-USER> (defun our-list-to-hash-set (list &key test)
           (declare (ignore test))
           (let ((hs (hash-set:make-hash-set)))
             (loop :for item :in list
                   :do (hash-set:hs-ninsert hs item))
             hs))
OUR-LIST-TO-HASH-SET
CL-USER> (let ((reader:*set-function* 'our-list-to-hash-set))
           (hash-set:dohashset (x (eval (read-from-string "#{(list 1 2)}")))
             (princ x)))
(1 2)
NIL

@mx
Copy link
Author

mx commented Dec 10, 2021

With the latest commit removing the hash-set dependency, I suppose this issue is now obsolete.

That said, before closing it, since you already went through the work of writing a version that works. I'm wondering if it makes sense to you to add a contrib/ directory, with a system for hash-set, that you include/use when you want to use hash-set with this library. Then pull requests could be made similarly for adding systems for fset and whatever other data structure libraries the user cares for.

@digikar99
Copy link
Owner

Before adding a contrib, I'd want to evaluate cl-syntax to see if reader can be merged into it. The original cl-syntax has been archived by the author, so a maintainer (someone who has already invested time in it and uses it actively) for cl-syntax is remaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants