16 Jul 2013, 02:25 Dave Thomas (338 posts) Write a function that takes a list of dqs and prints each on a separate line, centered in a column which is the width of the longest. Make sure it works with UTF characters. ``````iex> center(["cat", "zebra", "elephant"]) cat zebra elephant `````` A Possible Solution ```defmodule MyString do def center(strings) do strings |> Enum.map_reduce(0, accumulate_max_length(&1, &2)) |> center_strings_in_field |> Enum.each(IO.puts(&1)) end # We jump through this hoop to avoid calculating the string length twice. # Here, we build a tuple containing the length and the string, and # nest it in a tuple containing the maximum length seen so far defp accumulate_max_length(string, max_length_so_far) do l = String.length(string) { {string, l}, max(l, max_length_so_far) } end defp center_strings_in_field({strings, field_width}) do strings |> Enum.map(center_one_string(field_width, &1)) end defp center_one_string(field_width, {string, length}) do %b[#{String.duplicate(" ", div(field_width - length, 2))}#{string}] end end MyString.center %w{ cat zebra elephant } ``` 31 Oct 2013, 15:24 Roger Turner (7 posts) `````` def center(strings) do strings |> center_strings |> Enum.each &IO.puts/1 end def center_strings(strings) do max_length = strings |> Enum.map(&String.length/1) |> Enum.max in_width = fn (string) -> l = String.length string div(max_length - l, 2) + l end strings |> Enum.map &(String.rjust(&1, in_width.(&1))) end `````` 14 Jan 2014, 01:41 Daniel Ashton (7 posts) ``````defmodule Binaries do import Enum, only: [max: 1, map: 2, each: 2] import String, only: [length: 1, duplicate: 2] import Kernel, except: [length: 1] def center(str_list) do max_len = str_list |> map(&(length &1)) |> max str_list |> each &(IO.puts duplicate(" ", div(max_len - length(&1), 2)) <> &1) end end `````` 16 Jun 2014, 02:51 Luke Imhoff (6 posts) I found that Erlang has a :string library, but confusingly, there strings are character lists, but since we’re just calling IO.puts and it doesn’t care, you can use :string.centre (note British English spelling even though the docs will say ‘center’ in the description). ``````defmodule DoubleQuotedStrings do def center(list_of_strings) do max_length = list_of_strings |> Enum.map(&String.length/1) |> Enum.max list_of_strings |> Enum.map(&String.to_char_list/1) |> Enum.map(&(:string.centre(&1, max_length))) |> Enum.map(&IO.puts/1) end end `````` 14 Aug 2014, 09:54 Jeremy Ozog (1 post) `````` def center(strings) do maxlength = strings |> Enum.max_by(&String.length/1) |> String.length Enum.map(strings, &(display_padded_string(&1, maxlength))) end defp display_padded_string(str, maxlength), do: IO.puts String.rjust(str, get_padding(str, maxlength)) defp get_padding(str, maxlength), do: String.length(str) + div(maxlength-String.length(str), 2) `````` 22 Aug 2014, 09:27 Peng Xiang (1 post) It seems I still don’t get the sprit of functional programming… ``````defmodule StringList do def center string_list do max_length = string_list |> Enum.map(&String.length/1) |> Enum.max Enum.each string_list, fn str -> IO.puts String.rjust str, div(max_length+String.length(str),2) end end end StringList.center ["hello", "world", "Hahahahaha"] `````` You must be logged in to comment