From cb858bc636570918eafc083e7b196ab94f6f0b74 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 13 Dec 2018 11:46:03 -0800 Subject: [PATCH] replace lazy-thunk with delay / force --- xenomorph/xenomorph/redo/helper.rkt | 4 +--- xenomorph/xenomorph/redo/pointer.rkt | 3 ++- xenomorph/xenomorph/redo/struct.rkt | 9 +++++++-- xenomorph/xenomorph/redo/test/pointer-test.rkt | 3 ++- xenomorph/xenomorph/redo/versioned-struct.rkt | 1 + 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/xenomorph/xenomorph/redo/helper.rkt b/xenomorph/xenomorph/redo/helper.rkt index b972a62b..12d0026a 100644 --- a/xenomorph/xenomorph/redo/helper.rkt +++ b/xenomorph/xenomorph/redo/helper.rkt @@ -56,6 +56,4 @@ (cond [(void? size) 0] [(and (integer? size) (not (negative? size))) size] - [else (raise-argument-error 'size "nonnegative integer" size)])) - -(struct lazy-thunk (proc) #:transparent) \ No newline at end of file + [else (raise-argument-error 'size "nonnegative integer" size)])) \ No newline at end of file diff --git a/xenomorph/xenomorph/redo/pointer.rkt b/xenomorph/xenomorph/redo/pointer.rkt index 4e317d30..83d96131 100644 --- a/xenomorph/xenomorph/redo/pointer.rkt +++ b/xenomorph/xenomorph/redo/pointer.rkt @@ -1,6 +1,7 @@ #lang debug racket/base (require "helper.rkt" racket/dict + racket/promise sugar/unstable/dict) (provide (all-defined-out)) @@ -42,7 +43,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee (pos port orig-pos) val])) (if (pointer-lazy? xp) - (lazy-thunk decode-value) + (delay (decode-value)) (decode-value))] [else ptr])]))) diff --git a/xenomorph/xenomorph/redo/struct.rkt b/xenomorph/xenomorph/redo/struct.rkt index 48cd122a..b29dfa97 100644 --- a/xenomorph/xenomorph/redo/struct.rkt +++ b/xenomorph/xenomorph/redo/struct.rkt @@ -1,5 +1,10 @@ #lang debug racket/base -(require (prefix-in d: racket/dict) racket/list "helper.rkt" "number.rkt" sugar/unstable/dict) +(require (prefix-in d: racket/dict) + racket/promise + racket/list + "helper.rkt" + "number.rkt" + sugar/unstable/dict) (provide (all-defined-out)) #| @@ -19,7 +24,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee [(define (dict-set! d k v) (d:dict-set! (choose-dict d k) k v)) (define (dict-ref d k [thunk #f]) (define res (d:dict-ref (choose-dict d k) k thunk)) - (if (lazy-thunk? res) ((lazy-thunk-proc res)) res)) + (force res)) (define (dict-remove! d k) (d:dict-remove! (choose-dict d k) k)) ;; public keys only (define (dict-keys d) (d:dict-keys (struct-dict-res-_kv d))) diff --git a/xenomorph/xenomorph/redo/test/pointer-test.rkt b/xenomorph/xenomorph/redo/test/pointer-test.rkt index a677c23b..b828cedd 100644 --- a/xenomorph/xenomorph/redo/test/pointer-test.rkt +++ b/xenomorph/xenomorph/redo/test/pointer-test.rkt @@ -5,6 +5,7 @@ "../pointer.rkt" "../number.rkt" "../struct.rkt" + racket/promise sugar/unstable/dict) #| @@ -59,7 +60,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee "decode should support decoding pointers lazily" (parameterize ([current-input-port (open-input-bytes (bytes 1 53))]) (define res (decode (+xstruct (dictify 'ptr (+xpointer uint8 uint8 (mhasheq 'lazy #t)))))) - (check-true (lazy-thunk? (dict-ref (struct-dict-res-_kv res) 'ptr))) + (check-true (promise? (dict-ref (struct-dict-res-_kv res) 'ptr))) (check-equal? (dict-ref res 'ptr) 53))) (test-case diff --git a/xenomorph/xenomorph/redo/versioned-struct.rkt b/xenomorph/xenomorph/redo/versioned-struct.rkt index 1c4fdbd2..b3ef3c81 100644 --- a/xenomorph/xenomorph/redo/versioned-struct.rkt +++ b/xenomorph/xenomorph/redo/versioned-struct.rkt @@ -15,6 +15,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee (dict-set! res 'version (cond + [(integer? (xversioned-struct-type xvs)) (xversioned-struct-type xvs)] #;[forced-version] ; for testing purposes: pass an explicit version [(or (symbol? (xversioned-struct-type xvs)) (procedure? (xversioned-struct-type xvs))) (unless parent