From 8a85b5236247e82775384dc08db82eb7a7b6cc8b Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 16 Jun 2019 09:45:54 -0700 Subject: [PATCH] dict and vdict examples --- .../xenomorph/scribblings/xenomorph.scrbl | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/xenomorph/xenomorph/scribblings/xenomorph.scrbl b/xenomorph/xenomorph/scribblings/xenomorph.scrbl index 44639f4a..16e99fb2 100644 --- a/xenomorph/xenomorph/scribblings/xenomorph.scrbl +++ b/xenomorph/xenomorph/scribblings/xenomorph.scrbl @@ -1274,7 +1274,22 @@ The rest arguments determine the keys and value types of the dict. These argumen @racket[pre-encode-proc] and @racket[post-decode-proc] control the pre-encoding and post-decoding procedures, respectively. Each takes as input the value to be processed and returns a new value. -@racket[base-class] controls the class used for instantiation of the new object. +@racket[base-class] controls the class used for instantiation of the new object. + +@examples[#:eval my-eval +(define d1 (x:dict 'foo uint8 'bar (x:string #:length 5))) +(define d1-vals (hasheq 'foo 42 'bar "hello")) +(encode d1 d1-vals #f) +(decode d1 #"*hello") + +(define d2 (x:dict 'zam (x:list #:length 3 #:type uint8) + 'nested d1)) +(define d2-vals (hasheq 'zam '(42 43 44) + 'nested d1-vals)) +(encode d2 d2-vals #f) +(decode d2 #"*+,*hello") +] + } @@ -1357,7 +1372,30 @@ Generate an instance of @racket[x:versioned-dict%] (or a subclass of @racket[x:v @racket[pre-encode-proc] and @racket[post-decode-proc] control the pre-encoding and post-decoding procedures, respectively. Each takes as input the value to be processed and returns a new value. -@racket[base-class] controls the class used for instantiation of the new object. +@racket[base-class] controls the class used for instantiation of the new object. + +@examples[#:eval my-eval +(define d1 (x:dict 'foo uint8 'bar (x:string #:length 5))) +(define d1-vals (hasheq 'foo 42 'bar "hello" 'my-version-key 'd1)) + +(define d2 (x:dict 'zam (x:list #:length 3 #:type uint8) + 'nested d1)) +(define d2-vals (hasheq 'zam '(42 43 44) + 'nested d1-vals + 'my-version-key 'd2)) + +(define vdict (x:versioned-dict + #:type (x:symbol) + #:version-key 'my-version-key + #:versions (hash 'd1 d1 'd2 d2))) + +(encode vdict d1-vals #f) +(decode vdict #"d1\0*hello") + +(encode vdict d2-vals #f) +(decode vdict #"d2\0*+,*hello") +] + } @subsubsection{Reserved values}