Define Private Keys in a Hash using a Struct-Like Interface
hash-partition
hash-partition-out
7.8

Define Private Keys in a Hash using a Struct-Like Interface

Sage Gerard

 (require hash-partition) package: hash-partition

This library defines a hash partition as a struct-like interface for uninterned symbol keys in a hash table.

(require hash-partition
         racket/contract
         racket/hash)
 
(hash-partition editorial (title summary))
(hash-partition news-article (title summary))
 
(provide (hash-partition-out editorial
                             [title string?]
                             [summary (or/c #f string?)])
         (hash-partition-out news-article
                             [title string?]
                             [summary (or/c #f string?)]))
 
(define journalism/in-practice
  (editorial "Evil Eggo Euthanizes the Earnest"
             "How waffles killed my dog."))
 
(define journalism/in-theory
  (news-article "Smyrna, GA Town Hall Summary: April 2017"
                "Here we summarize key decisions reached."))
 
; How waffles killed my dog.
(editorial-summary (hash-union journalism/in-practice journalism/in-theory))

syntax

(hash-partition constructor item ...)

Defines 2+n bindings as shown:

(hash-partition point (x y z))
 
(define p (point 0 1 2)) ; constructor
 
; Predicate
(point? p)
 
; Accessors
(point-x p)
(point-y p)
(point-z p)

The constructor accepts as many arguments as there are items. The predicate only checks that the argument is a hash that defines all keys created by the constructor.

There are no fail thunks for accessors because they are only meant to be used for values that match the defined predicate.

syntax

(hash-partition-out id [item-id contract-expr] ...)

A provide sub-form for hash partitions. Supports field-level contracts.