main
Matthew Butterick 6 years ago
parent 24208e3e24
commit 0608024893

@ -99,15 +99,6 @@
[(partial (list (? nonprinting-in-middle-soft-break?) ... rest ...)) (append (or partial null) rest)]))
(define (finish-wraps wraps wrap-proc soft-break?)
(for/list ([wrap (in-list wraps)])
(match wrap
[(list (? nonprinting-at-end?)) wrap] ; matches break signals
;; pieces will have been accumulated in reverse order
;; thus beginning of list represents the end of the wrap
[(list (? (conjoin soft-break? nonprinting-at-end?)) ... rest ...)
(wrap-proc (reverse rest))])))
(define (break-softs qs
target-size
debug
@ -121,7 +112,15 @@
[qs qs] ; list of quads
#:result (let ()
(define last-wrap (wrap-append #false (wrap-append next-wrap-tail next-wrap-head)))
(finish-wraps (cons last-wrap wraps) finish-wrap-proc soft-break?)))
(define finished-wraps
(for/list ([wrap (in-list (cons last-wrap wraps))])
(match wrap
[(list (? nonprinting-at-end?)) wrap] ; matches break signals
;; pieces will have been accumulated in reverse order
;; thus beginning of list represents the end of the wrap
[(list (? (conjoin soft-break? nonprinting-at-end?)) ... rest ...)
(finish-wrap-proc (reverse rest))])))
(add-between finished-wraps (list break-val))))
([i (in-naturals)]
#:break (empty? qs))
(match-define (cons q other-qs) qs)
@ -155,13 +154,13 @@
other-qs)]
[(and would-overflow? (empty? next-wrap-head))
(debug-report 'would-overflow-hard-without-captured-break)
(values (list* (list break-val) next-wrap-tail wraps)
(values (cons next-wrap-tail wraps)
null
null
#false
qs)]
[would-overflow? ; finish the wrap & reset the line without consuming a quad
(values (list* (list break-val) next-wrap-head wraps)
(values (cons next-wrap-head wraps)
null
next-wrap-tail
(apply + (map distance next-wrap-tail))
@ -392,3 +391,5 @@
(check-equal? (linewrap2 (list x x x x) 3) (list (slug x x x) 'lb (slug x)))
(check-equal? (linewrap2 (list x x x sp x x) 2) (list (slug x x) 'lb (slug x) 'lb (slug x x)))
(check-equal? (linewrap2 (list x x x sp x x) 3) (list (slug x x x) 'lb (slug x x)))))
#;(time-avg 100 (void (visual-wrap "The make-object procedure creates a new object with by-position initialization arguments, the new form creates a new object with by-name initialization arguments, and the instantiate form creates a new object with both by-position and by-name initialization arguments. All fields in the newly created object are initially bound to the special #<undefined> value (see Void). Initialization variables with default value expressions (and no provided value) are also initialized to #<undefined>. After argument values are assigned to initialization variables, expressions in field clauses, init-field clauses with no provided argument, init clauses with no provided argument, private field definitions, and other expressions are evaluated. Those expressions are evaluated as they appear in the class expression, from left to right. Sometime during the evaluation of the expressions, superclass-declared initializations must be evaluated once by using the super-make-object procedure, super-new form, or super-instantiate form. By-name initialization arguments to a class that have no matching initialization variable are implicitly added as by-name arguments to a super-make-object, super-new, or super-instantiate invocation, after the explicit arguments. If multiple initialization arguments are provided for the same name, the first (if any) is used, and the unused arguments are propagated to the superclass. (Note that converted by-position arguments are always placed before explicit by-name arguments.) The initialization procedure for the object% class accepts zero initialization arguments; if it receives any by-name initialization arguments, then exn:fail:object exception is raised. If the end of initialization is reached for any class in the hierarchy without invoking the superclasss initialization, the exn:fail:object exception is raised. Also, if superclass initialization is invoked more than once, the exn:fail:object exception is raised. Fields inherited from a superclass are not initialized until the superclasss initialization procedure is invoked. In contrast, all methods are available for an object as soon as the object is created; the overriding of methods is not affected by initialization (unlike objects in C++)." 35)))

@ -0,0 +1,131 @@
#lang debug racket
(require sugar/debug)
(define words "This tutorial provides a brief introduction to the Racket programming language by using one of its picture-drawing libraries. Even if you dont intend to use Racket for your artistic endeavours, the picture library supports interesting and enlightening examples. After all, a picture is worth five hundred “hello world”s.")
(define (string->widths words)
(add-between (map string-length (string-split words)) 1))
(define wws (string->widths words))
(define (greedy-split xs width)
(for/fold ([xss null]
[xs null]
#:result (reverse (cons xs xss)))
([x (in-list xs)])
(define next-xs (cons x xs))
(if (<= (apply + next-xs) width)
(values xss next-xs)
(values (cons (reverse xs) xss) (list x)))))
wws
(require rackunit)
(define width 30)
(greedy-split wws width)
(define (optimal-score xs width)
(cond
[(empty? xs) 0]
[(= (length xs) 1) (car xs)]
[else
(for/fold ([scores null]
[xscores null]
[widths (list 0)]
#:result (cadr scores))
([(x idx) (in-indexed xs)]
#:break (and (pair? xscores) (negative? (car xscores))))
(define next-width (+ x (car widths)))
(define next-xscore (- width next-width))
(define next-score (+ next-xscore (optimal-score (drop xs (add1 idx)) width)))
(values (cons next-score scores)
(cons next-xscore xscores)
(cons next-width widths)))]))
(optimal-score wws width)
#;(time-avg 100 (void (check-equal? (greedy-split (string->widths
"The make-object procedure creates a new object with by-position initialization arguments, the new form creates a new object with by-name initialization arguments, and the instantiate form creates a new object with both by-position and by-name initialization arguments. All fields in the newly created object are initially bound to the special #<undefined> value (see Void). Initialization variables with default value expressions (and no provided value) are also initialized to #<undefined>. After argument values are assigned to initialization variables, expressions in field clauses, init-field clauses with no provided argument, init clauses with no provided argument, private field definitions, and other expressions are evaluated. Those expressions are evaluated as they appear in the class expression, from left to right. Sometime during the evaluation of the expressions, superclass-declared initializations must be evaluated once by using the super-make-object procedure, super-new form, or super-instantiate form. By-name initialization arguments to a class that have no matching initialization variable are implicitly added as by-name arguments to a super-make-object, super-new, or super-instantiate invocation, after the explicit arguments. If multiple initialization arguments are provided for the same name, the first (if any) is used, and the unused arguments are propagated to the superclass. (Note that converted by-position arguments are always placed before explicit by-name arguments.) The initialization procedure for the object% class accepts zero initialization arguments; if it receives any by-name initialization arguments, then exn:fail:object exception is raised. If the end of initialization is reached for any class in the hierarchy without invoking the superclasss initialization, the exn:fail:object exception is raised. Also, if superclass initialization is invoked more than once, the exn:fail:object exception is raised. Fields inherited from a superclass are not initialized until the superclasss initialization procedure is invoked. In contrast, all methods are available for an object as soon as the object is created; the overriding of methods is not affected by initialization (unlike objects in C++).") 30)
'((3 1 11 1 9 1)
(7 1 1 1 3 1 6 1 4 1)
(11 1 14 1)
(10 1 3 1 3 1 4 1)
(7 1 1 1 3 1 6 1 4 1)
(7 1 14 1)
(10 1 3 1 3 1 11)
(1 4 1 7 1 1 1 3 1 6 1)
(4 1 4 1 11 1 3 1)
(7 1 14 1)
(10 1 3 1 6 1 2 1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