reformulate sep

This commit is contained in:
mehbark 2025-01-28 21:22:16 -05:00
parent f03131610f
commit 611cc2a281

View file

@ -14,14 +14,14 @@ def String.replicate : Nat → Char → String
| 0, _ => "" | 0, _ => ""
| n+1, x => replicate n x |>.push x | n+1, x => replicate n x |>.push x
def sep (xs space : Array String) def sep (xs : Array String) (space : Fin (xs.size-1) → String)
: String := Id.run do : String := Id.run do
let mut out := "" let mut out := ""
let mut i := 0 let mut i := 0
while h : i < xs.size do while h : i < xs.size do
out := out ++ xs[i] out := out ++ xs[i]
if let some space := space[i]? if h : i < xs.size - 1 then
then out := out ++ space out := out ++ space ⟨i, h⟩
i := i + 1 i := i + 1
return out return out
@ -34,11 +34,9 @@ def justify : Nat → Array String → String
let smallSpaceLen := spacesNeeded / numSpaces let smallSpaceLen := spacesNeeded / numSpaces
let smallSpace := String.replicate smallSpaceLen ' ' let smallSpace := String.replicate smallSpaceLen ' '
let largeSpaces := spacesNeeded % numSpaces let largeSpaces := spacesNeeded % numSpaces
let smallSpaces := numSpaces - largeSpaces
let largeSpace := smallSpace.push ' ' let largeSpace := smallSpace.push ' '
let spaces := Array.mkArray largeSpaces largeSpace let space i := if i.val ≤ largeSpaces then largeSpace else smallSpace
++ Array.mkArray smallSpaces smallSpace sep words space
sep words spaces
def breakUp (width : Nat) (words : Array String) : Array (Array String) := def breakUp (width : Nat) (words : Array String) : Array (Array String) :=
let (_, line, lines) := words.foldl let (_, line, lines) := words.foldl