tangled
alpha
login
or
join now
bernsteinbear.com
/
box.py
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
Add fold_left
bernsteinbear.com
9 months ago
d966cf9a
0e295024
+8
-12
1 changed file
expand all
collapse all
unified
split
box.py
+8
-12
box.py
···
64
64
case "Center": return vconcat([space(ph//2, bw), b, space((ph - ph//2), bw)])
65
65
case _: raise ValueError("invalid vertical alignment {align}")
66
66
67
67
+
def fold_left(f, result, items):
68
68
+
for item in items:
69
69
+
result = f(result, item)
70
70
+
return result
71
71
+
67
72
def hconcat(boxes: list[T], align: Vertical = "Center") -> T:
68
68
-
result = empty()
69
69
-
for box in boxes:
70
70
-
result = beside(result, box, align)
71
71
-
return result
73
73
+
return fold_left(lambda acc, item: beside(acc, item, align), empty(), boxes)
72
74
73
75
def vconcat(boxes: list[T], align: Horizontal = "Center") -> T:
74
74
-
result = empty()
75
75
-
for box in boxes:
76
76
-
result = above(result, box, align)
77
77
-
return result
76
76
+
return fold_left(lambda acc, item: above(acc, item, align), empty(), boxes)
78
77
79
78
def grid(g: list[list[T]]) -> T: return vconcat([hconcat(x) for x in g])
80
79
···
169
168
return vconcat([header, hbar, column])
170
169
titled = [make_title(h, column) for h, column in cols]
171
170
vbar = fill("|", height(titled[0]), 1)
172
172
-
result = vbar
173
173
-
for col in titled:
174
174
-
result = hconcat([result, col, vbar])
175
175
-
return result
171
171
+
return fold_left(lambda acc, col: hconcat([acc, col, vbar]), vbar, titled)
176
172
177
173
class ExampleTests(unittest.TestCase):
178
174
def test_sierpinski(self):