racketcontract

how to make contract for a non-empty hashtable


Racket has a non-empty-listof contract but no non-empty-hashof contract. Is there a way to build one?


Solution

  • Predicates can be used as contracts, so:

    (define (non-empty-hash? x)
      (and (hash? x)
           (not (hash-empty? x))))
    

    Then you can use non-empty-hash? as your contract.