Skip to content

Commit 89a6de5

Browse files
Copilotjackfirth
andcommitted
Address code review feedback: fix comments and simplify unflatten logic
Co-authored-by: jackfirth <[email protected]>
1 parent f874255 commit 89a6de5

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

private/syntax-path.rkt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@
364364
(check-equal? (syntax->datum actual) 'FOO))
365365

366366
(test-case "improper list with nested access"
367-
; #'(a b . (c FOO e)) flattens to (a b c FOO e) where c FOO e is the tail
368-
; But the tail is itself a syntax object wrapping (c FOO e)
369-
; So at index 2, we get the whole (c FOO e) syntax
367+
; #'(a b . (c FOO e)) flattens to (a b (c FOO e))
368+
; The tail (c FOO e) is a syntax object at index 2
369+
; So at index 2, we get the whole (c FOO e) syntax, then index 1 within that gives FOO
370370
(define stx #'(a b . (c FOO e)))
371371
(define actual (syntax-ref stx (syntax-path (list 2 1))))
372372
(check-equal? (syntax->datum actual) 'FOO))
@@ -400,6 +400,8 @@
400400
(define unwrapped (syntax-e stx))
401401
(cond
402402
; Handle improper lists - flatten, update, and reconstruct
403+
; Note: This does flatten/unflatten on each access, which could be optimized if needed
404+
; for deeply nested paths, but improper lists are relatively rare in practice.
403405
[(and (pair? unwrapped) (not (list? unwrapped)))
404406
(define flattened (flatten-improper-list unwrapped))
405407
(define updated-elem (loop (list-ref flattened i) remaining-elements))
@@ -451,16 +453,19 @@
451453
(define (unflatten-improper-list flattened original)
452454
(cond
453455
[(null? flattened) '()]
454-
[(null? (cdr flattened)) (car flattened)]
455456
[(and (pair? original) (not (list? original)))
456457
; Reconstruct the improper structure
457458
(let unflatten-with-tail ([flat flattened] [orig original])
458459
(cond
459-
[(null? (cdr flat)) (car flat)]
460-
[(pair? orig)
461-
(cons (car flat) (unflatten-with-tail (cdr flat) (cdr orig)))]
462-
[else (car flat)]))]
463-
[else flattened]))
460+
[(or (null? (cdr flat)) (not (pair? orig)))
461+
; Base case: last element or reached non-pair in original
462+
(car flat)]
463+
[else
464+
; Recursive case: continue building the improper list
465+
(cons (car flat) (unflatten-with-tail (cdr flat) (cdr orig)))]))]
466+
[else
467+
; For proper lists or single elements, return as-is
468+
flattened]))
464469

465470

466471

0 commit comments

Comments
 (0)