From e839167ef151d4a6d210329616faa3048c41bf68 Mon Sep 17 00:00:00 2001 From: preciz Date: Fri, 25 Sep 2026 09:15:08 +0200 Subject: [PATCH] Calculate IEx table widths with Enum.reduce_while Calculate the maximum grapheme length directly in print_table/2 using Enum.reduce_while/3, eliminating the intermediate lengths list and the single-use max_length/1 helper while halting traversal early as soon as a length reaches the 30-character column clamp. Assisted-by: Codex:GPT-6 Assisted-by: Antigravity:Gemini 3.8 Flash --- lib/iex/lib/iex/helpers.ex | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/iex/lib/iex/helpers.ex b/lib/iex/lib/iex/helpers.ex index 3e37ed70824..99eccaff6fd 100644 --- a/lib/iex/lib/iex/helpers.ex +++ b/lib/iex/lib/iex/helpers.ex @@ -1139,9 +1139,15 @@ defmodule IEx.Helpers do defp print_table(list, printer) do # print items in multiple columns (2 columns in the worst case) - lengths = Enum.map(list, &String.length(&1)) - max_length = max_length(lengths) - offset = min(max_length, 30) + 4 + max_length = + Enum.reduce_while(list, 0, fn item, acc -> + case String.length(item) do + len when len >= 30 -> {:halt, 30} + len -> {:cont, max(acc, len)} + end + end) + + offset = max_length + 4 print_table(list, printer, offset) end @@ -1165,10 +1171,6 @@ defmodule IEx.Helpers do IO.puts("") end - defp max_length(list) do - Enum.reduce(list, 0, &max(&1, &2)) - end - defp format_item(path, representation) do case File.stat(path) do {:ok, %File.Stat{type: :device}} ->