]> gitweb.factorcode.org Git - factor.git/blob - basis/wrap/wrap.factor
Making lazy lists compile, and using them where applicable
[factor.git] / basis / wrap / wrap.factor
1 USING: kernel sequences math arrays locals fry accessors
2 lists splitting call make combinators.short-circuit namespaces
3 grouping splitting.monotonic ;
4 IN: wrap
5
6 <PRIVATE
7
8 ! black is the text length, white is the whitespace length
9 TUPLE: element contents black white ;
10 C: <element> element
11
12 : element-length ( element -- n )
13     [ black>> ] [ white>> ] bi + ;
14
15 : swons ( cdr car -- cons )
16     swap cons ;
17
18 : unswons ( cons -- cdr car )
19     [ cdr ] [ car ] bi ;
20
21 : 1list? ( list -- ? )
22     { [ ] [ cdr +nil+ = ] } 1&& ;
23
24 : lists>arrays ( lists -- arrays )
25     [ list>seq ] lmap>array ;
26
27 TUPLE: paragraph lines head-width tail-cost ;
28 C: <paragraph> paragraph
29
30 SYMBOL: line-max
31 SYMBOL: line-ideal
32
33 : deviation ( length -- n )
34     line-ideal get - sq ;
35
36 : top-fits? ( paragraph -- ? )
37     [ head-width>> ]
38     [ lines>> 1list? line-ideal line-max ? get ] bi <= ;
39
40 : fits? ( paragraph -- ? )
41     ! Make this not count spaces at end
42     { [ lines>> car 1list? ] [ top-fits? ] } 1|| ;
43
44 :: min-by ( seq quot -- elt )
45     f 1.0/0.0 seq [| key value new |
46         new quot call :> newvalue
47         newvalue value < [ new newvalue ] [ key value ] if
48     ] each drop ; inline
49
50 : paragraph-cost ( paragraph -- cost )
51     [ head-width>> deviation ]
52     [ tail-cost>> ] bi + ;
53
54 : min-cost ( paragraphs -- paragraph )
55     [ paragraph-cost ] min-by ;
56
57 : new-line ( paragraph element -- paragraph )
58     [ [ lines>> ] [ 1list ] bi* swons ]
59     [ nip black>> ]
60     [ drop paragraph-cost ] 2tri
61     <paragraph> ;
62
63 : glue ( paragraph element -- paragraph )
64     [ [ lines>> unswons ] dip swons swons ]
65     [ [ head-width>> ] [ element-length ] bi* + ]
66     [ drop tail-cost>> ] 2tri
67     <paragraph> ;
68
69 : wrap-step ( paragraphs element -- paragraphs )
70     [ '[ _ glue ] map ]
71     [ [ min-cost ] dip new-line ]
72     2bi prefix
73     [ fits? ] filter ;
74
75 : 1paragraph ( element -- paragraph )
76     [ 1list 1list ]
77     [ black>> ] bi
78     0 <paragraph> ;
79
80 : post-process ( paragraph -- array )
81     lines>> lists>arrays
82     [ [ contents>> ] map ] map ;
83
84 : initialize ( elements -- elements paragraph )
85     <reversed> unclip-slice 1paragraph 1array ;
86
87 : wrap ( elements line-max line-ideal -- paragraph )
88     [
89         line-ideal set
90         line-max set
91         initialize
92         [ wrap-step ] reduce
93         min-cost
94         post-process
95     ] with-scope ;
96
97 PRIVATE>
98
99 TUPLE: segment key width break? ;
100 C: <segment> segment
101
102 <PRIVATE
103
104 : segments-length ( segments -- length )
105     [ width>> ] map sum ;
106
107 : make-element ( whites blacks -- element )
108     [ append ] [ [ segments-length ] bi@ ] 2bi <element> ;
109  
110 : ?first2 ( seq -- first/f second/f )
111     [ 0 swap ?nth ]
112     [ 1 swap ?nth ] bi ;
113
114 : split-segments ( seq -- half-elements )
115     [ [ break?>> ] bi@ = ] monotonic-split ;
116
117 : ?first-break ( seq -- newseq f/element )
118     dup first first break?>>
119     [ unclip-slice f swap make-element ]
120     [ f ] if ;
121
122 : make-elements ( seq f/element -- elements )
123     [ 2 <groups> [ ?first2 make-element ] map ] dip
124     [ prefix ] when* ;
125
126 : segments>elements ( seq -- newseq )
127     split-segments ?first-break make-elements ;
128
129 PRIVATE>
130
131 : wrap-segments ( segments line-max line-ideal -- lines )
132     [ segments>elements ] 2dip wrap [ concat ] map ;
133
134 <PRIVATE
135
136 : split-lines ( string -- elements-lines )
137     string-lines [
138         " \t" split harvest
139         [ dup length 1 <element> ] map
140     ] map ;
141
142 : join-elements ( wrapped-lines -- lines )
143     [ " " join ] map ;
144
145 : join-lines ( strings -- string )
146     "\n" join ;
147
148 PRIVATE>
149
150 : wrap-lines ( lines width -- newlines )
151     [ split-lines ] dip '[ _ dup wrap join-elements ] map concat ;
152
153 : wrap-string ( string width -- newstring )
154     wrap-lines join-lines ;
155
156 : wrap-indented-string ( string width indent -- newstring )
157     [ length - wrap-lines ] keep '[ _ prepend ] map join-lines ;