idiomatic tweaks

This commit is contained in:
mehbark 2025-02-04 10:10:36 -05:00
parent 611cc2a281
commit 19adece4a2
3 changed files with 25 additions and 25 deletions

View file

@ -3,12 +3,8 @@ def String.words (s : String) :=
def String.lines (s : String) := def String.lines (s : String) :=
s.splitOn "\n" |>.filter (¬·.isEmpty) |>.toArray s.splitOn "\n" |>.filter (¬·.isEmpty) |>.toArray
-- TODO: remove trailing def Array.unlines (xs : Array String) : String :=
def Array.unlines (xs : Array String) : String := Id.run do xs.foldl (· ++ · ++ "\n") ""
let mut out := ""
for line in xs do
out := out ++ line ++ "\n"
return out
def String.replicate : Nat → Char → String def String.replicate : Nat → Char → String
| 0, _ => "" | 0, _ => ""
@ -25,22 +21,21 @@ def sep (xs : Array String) (space : Fin (xs.size-1) → String)
i := i + 1 i := i + 1
return out return out
def justify : Nat → Array String → String def justify (width : Nat) (words : Array String) : String :=
| _, #[word] => word sep words space
| width, words => where
let wordsLength := words.foldr (·.length + ·) 0 space i := if i.val ≤ largeSpaces then largeSpace else smallSpace
let spacesNeeded := width - wordsLength largeSpace := smallSpace.push ' '
let numSpaces := words.size - 1 smallSpace := String.replicate smallSpaceLen ' '
let smallSpaceLen := spacesNeeded / numSpaces smallSpaceLen := spacesNeeded / numSpaces
let smallSpace := String.replicate smallSpaceLen ' ' largeSpaces := spacesNeeded % numSpaces
let largeSpaces := spacesNeeded % numSpaces numSpaces := words.size - 1
let largeSpace := smallSpace.push ' ' spacesNeeded := width - wordsLength
let space i := if i.val ≤ largeSpaces then largeSpace else smallSpace wordsLength := words.foldr (·.length + ·) 0
sep words space
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
(λ(left, line, lines) word ↦ (fun (left, line, lines) word =>
if word.length < left if word.length < left
then (left - (word.length + 1), line.push word, lines) then (left - (word.length + 1), line.push word, lines)
else (width - word.length, #[word], lines.push line)) else (width - word.length, #[word], lines.push line))
@ -54,20 +49,20 @@ def usage := "Usage: justify WIDTH"
def fail : IO UInt32 := do IO.println usage; return 1 def fail : IO UInt32 := do IO.println usage; return 1
def main : List String → IO UInt32 def main : List String → IO UInt32
| [width] => | [width] => do
if let some width := width.toNat? if let some width := width.toNat?
then do then
let stdin ← IO.getStdin let stdin ← IO.getStdin
let mut remainder : Array String := #[] let mut remainder : Array String := #[]
while true do repeat
let line ← stdin.getLine let line ← stdin.getLine
if line.isEmpty then break if line.isEmpty then break
let lines := breakUp width (remainder ++ line.words) let lines := breakUp width (remainder ++ line.words)
if h : lines.size > 0 then if h : lines.size > 0 then
remainder := lines[lines.size-1] remainder := lines[lines.size-1]
for line in lines[0:lines.size-1] do for line in lines[0:lines.size-1] do
IO.println $ justify width line IO.println <| justify width line
IO.println $ String.intercalate " " remainder.toList IO.println <| sep remainder (fun _ => " ")
return 0 return 0
else fail else fail
| _ => fail | _ => fail

View file

@ -5,3 +5,8 @@ defaultTargets = ["justify"]
[[lean_exe]] [[lean_exe]]
name = "justify" name = "justify"
root = "Main" root = "Main"
[[require]]
name = "batteries"
scope = "leanprover-community"
rev = "main"

View file

@ -1 +1 @@
leanprover/lean4:4.15.0 leanprover/lean4:v4.17.0-rc1