15 Jul 2013, 03:42

Dave Thomas (337 posts)

  • (Tricky) Before reading the next section, see if you can write the code to format the data into columns, like the sample output at the start of the chapter. This is probably the longest piece of Elixir code you’ll have written. Try to do it without using if or cond.
29 Oct 2013, 16:48
Mac 128k logic board 96x96_pragsmall

Roger Turner (5 posts)

   defmodule Issues.Tabulate do

    def print_table_for_columns(list_of_dicts, columns) do

      cells = lc dict inlist list_of_dicts, do: 
        (lc {key, value} inlist Enum.to_list(dict), key in columns, do: 
            to_string(value) )

      headed = Enum.concat([columns], cells)

      lengths = lc row inlist headed, do: 
        Enum.map(row, &String.length/1)

      widths = lc col inlist List.zip(lengths), do: 

      padded = lc row inlist headed, do:
        (lc {cell, width} inlist Enum.zip(row, widths), do: 
          String.ljust(cell, width))

      divider = Enum.map(widths, &(String.duplicate("-", &1)))

      IO.puts Enum.join(Enum.at(padded, 0), " | ")
      IO.puts Enum.join(divider, "-+-")
      lc row inlist Enum.drop(padded, 1), do: IO.puts Enum.join(row, " | ")



  You must be logged in to comment