guicho271828 / trivia

Pattern Matcher Compatible with Optima
Other
334 stars 22 forks source link

HASH-TABLE-ENTRIES pattern. #122

Closed dop closed 3 years ago

dop commented 3 years ago

This tries to tackle issue #88.

I noticed that hash-table was already recognized as class and had to come up with different name, hence hash-table-entries. Let me know if there is better name to follow naming style of similar patterns.

Also added warnings for missing or odd number of arguments. Again, not sure if that's an appropriate style for this library.

bahmanm commented 3 years ago

very handy pattern...thanks. how can i test it out? i keep getting Symbol HASH-TABLE-ENTRIES is unbound in namespace PATTERN on both ccl and sbcl. for example

(let ((ht (make-hash-table :test #'equal)))
           (setf (gethash "a" ht) 10
                 (gethash "b" ht) 20)
           (trivia:match ht
             ((hash-table-entries "a" 10) 100)))

yields

Symbol HASH-TABLE-ENTRIES is unbound in namespace PATTERN

  0: (TRIVIA.LEVEL2:PATTERN-EXPAND-1 (HASH-TABLE-ENTRIES "a" 10))
  1: (TRIVIA.LEVEL2:PATTERN-EXPAND (HASH-TABLE-ENTRIES "a" 10))
  2: (TRIVIA.LEVEL2:PATTERN-EXPAND-ALL (HASH-TABLE-ENTRIES "a" 10))
  3: (TRIVIA.LEVEL2.IMPL::PATTERN-EXPAND-ALL/LIFT0 (HASH-TABLE-ENTRIES "a" 10))
  4: (TRIVIA.LEVEL2.IMPL::PATTERN-EXPAND-ALL/LIFT (HASH-TABLE-ENTRIES "a" 10))
  5: (TRIVIA.LEVEL2.IMPL::EXPAND-MULTIPATTERNS ((HASH-TABLE-ENTRIES "a" 10)))
  6: (TRIVIA.LEVEL2.IMPL::EXPAND-CLAUSE (((HASH-TABLE-ENTRIES "a" 10)) 100))
  7: (TRIVIA.LEVEL2:MATCH2*+ (TRIVIA.LEVEL2:MATCH2*+ (HT) (T) (((HASH-TABLE-ENTRIES "a" 10)) 100) ((TRIVIA.LEVEL2.IMPL::_) NIL)) #<CCL::LEXICAL-ENVIRONMENT #x302003D76F8D>)
  8: (FUNCALL #<Compiled-function TRIVIA.LEVEL2:MATCH2*+ Macroexpander #x3020039A493F> (TRIVIA.LEVEL2:MATCH2*+ (HT) (T) (((HASH-TABLE-ENTRIES "a" 10)) 100) ((TRIVIA.LEVEL2.IMPL::_) NIL)) #<CCL::LEXICAL-ENV..

i'm pretty sure i'm missing something obvious, though.

dop commented 3 years ago

For symbols that don't exist in cl package, like hash-table-entries, you'll have to import them or prefix them with package name.

(trivia:match (alexandria:plist-hash-table '("a" 1 "b" 2) :test 'equal)
  ((trivia:hash-table-entries "a" x) x))
guicho271828 commented 3 years ago

Thanks for the good contribution. Merged!