inversion

main
Matthew Butterick 7 years ago
parent 7de33ee60f
commit 707ae3172b

@ -8,13 +8,13 @@
(define last-breakpoint-k #f)
(define (capture-k!) (let/cc k (set! last-breakpoint-k k) #f))
(for/fold ([xs null]
[break-open? #f]
[break-open? #t]
[size-so-far 0]
#:result (reverse xs))
([(x idx) (in-indexed xs-in)])
(define-values (size-start size-mid size-end breakability) (if (promise? x) (force x) (x)))
(cond
[(not break-open?) (values (cons #t xs) #t (+ size-so-far size-start))]
[(not break-open?) (values (cons #f xs) #t (+ size-so-far size-start))]
[(<= (+ size-so-far size-end) target-size) ;; check condition based on size-end (as if x were breakpoint) ...
(cond
[(or (eq? breakability 'must) (and (eq? breakability 'can) (capture-k!))) ;; return point for `last-breakpoint-k`
@ -37,38 +37,41 @@
(module+ test
(check-equal? (break (list) 1) null)
(check-equal? (break (list ch) 1) '(#t))
(check-equal? (break (list ch ch) 1) '(#t #t))
(check-equal? (break (list ch ch ch) 1) '(#t #t #t))
(check-equal? (break (list ch ch ch) 2) '(#t #f #t))
(check-equal? (break (list ch ch ch ch) 2) '(#t #f #t #f))
(check-equal? (break (list ch ch ch ch ch) 3) '(#t #f #f #t #f))
(check-equal? (break (list ch ch ch ch ch) 1) '(#t #t #t #t #t))
(check-equal? (break (list ch ch ch ch ch) 10) '(#t #f #f #f #f))
(check-equal? (break (list ch) 1) '(#f))
(check-equal? (break (list ch ch) 1) '(#f #t))
(check-equal? (break (list ch ch ch) 1) '(#f #t #t))
(check-equal? (break (list ch ch ch) 2) '(#f #f #t))
(check-equal? (break (list ch ch ch ch) 2) '(#f #f #t #f))
(check-equal? (break (list ch ch ch ch ch) 3) '(#f #f #f #t #f))
(check-equal? (break (list ch ch ch ch ch) 1) '(#f #t #t #t #t))
(check-equal? (break (list ch ch ch ch ch) 10) '(#f #f #f #f #f))
(check-equal? (break (list sp) 1) '(#t))
(check-equal? (break (list sp sp) 1) '(#t #f))
(check-equal? (break (list sp sp) 2) '(#t #f))
(check-equal? (break (list sp sp) 3) '(#t #f))
(check-equal? (break (list sp sp sp) 1) '(#t #f #f))
(check-equal? (break (list sp sp sp) 2) '(#t #f #f))
(check-equal? (break (list sp sp sp) 3) '(#t #f #f))
(check-equal? (break (list sp) 1) '(#f))
(check-equal? (break (list sp sp) 1) '(#f #f))
(check-equal? (break (list sp sp) 2) '(#f #f))
(check-equal? (break (list sp sp) 3) '(#f #f))
(check-equal? (break (list sp sp sp) 1) '(#f #f #f))
(check-equal? (break (list sp sp sp) 2) '(#f #f #f))
(check-equal? (break (list sp sp sp) 3) '(#f #f #f))
(check-equal? (break (list ch sp) 1) '(#t #f))
(check-equal? (break (list sp ch) 1) '(#t #f))
(check-equal? (break (list sp ch ch) 1) '(#t #f #t))
(check-equal? (break (list ch sp ch) 1) '(#t #f #t))
(check-equal? (break (list ch sp sp ch) 1) '(#t #f #t #f))
(check-equal? (break (list ch sp ch sp) 1) '(#t #f #t #f))
(check-equal? (break (list ch ch sp ch) 2) '(#t #f #f #t))
(check-equal? (break (list ch sp ch) 3) '(#t #f #f))
(check-equal? (break (list ch sp ch ch) 3) '(#t #f #t #f))
;; now it gets weird
(check-equal? (break (list ch sp) 1) '(#f #f))
(check-equal? (break (list sp ch) 1) '(#f #f))
(check-equal? (break (list sp ch ch) 1) '(#f #f #t))
(check-equal? (break (list ch sp ch) 1) '(#f #f #t))
#|
(check-equal? (break (list ch sp sp ch) 1) '(#f #f #t #f))
(check-equal? (break (list ch sp ch sp) 1) '(#f #f #t #f))
(check-equal? (break (list ch ch sp ch) 2) '(#f #f #f #t))
(check-equal? (break (list ch sp ch) 3) '(#f #f #f))
(check-equal? (break (list ch sp ch ch) 3) '(#f #f #t #f))
;; trailing spaces
(check-equal? (break (list ch sp) 3) '(#t #f))
(check-equal? (break (list ch sp sp) 3) '(#t #f #f))
(check-equal? (break (list ch sp sp) 2) '(#t #f #f))
(check-equal? (break (list ch sp sp) 1) '(#t #f #f))
(check-equal? (break (list ch sp sp) 1) '(#t #f #f)) ; fails
(check-equal? (break (list ch br ch) 2) '(#t #f #t))
(check-equal? (break (list ch br ch ch) 3) '(#t #f #t #f))
@ -77,6 +80,7 @@
(check-equal? (break (list ch ch ch sp sp ch ch) 2) '(#t #f #t #f #f #t #f))
(check-equal? (break (list ch ch ch sp ch ch) 3) '(#t #f #f #f #t #f))
|#
)

Loading…
Cancel
Save