Skip to content

Instantly share code, notes, and snippets.

# evadne/xmastree.ex Last active Dec 16, 2019

Christmas Trees in Elixir
 defmodule Tree do def print(n) do for i <- 1 .. n do p(n - i, " ") p(i * 2 - 1, "*") w("\n") end p(n - 1, " ") w("|\n") end defp p(n, x) defp p(0, _), do: :ok defp p(n, x), do: for _ <- 1 .. n, do: w(x) defp w(x), do: IO.write(x) end defmodule BloatedTree do def print(n) do for i <- 1 .. n, do: p(d(n - i, " ") <> d(i * 2 - 1, "*")) p(d(n - 1, " ") <> "|") end defp d(n, s), do: String.duplicate(s, n) defp p(x), do: IO.puts(x) end defmodule SinglePrintTree do def print(n) do leaves = for i <- 1 .. n, do: d(n - i, " ") <> d(i * 2 - 1, "*") stem = d(n - 1, " ") <> "|" IO.puts(Enum.join(leaves ++ [stem], "\n")) end defp d(n, s), do: String.duplicate(s, n) end defmodule SinglePrintIOListTree do def print(n) do leaves = for i <- 1 .. n, do: [d(n - i, " "), d(i * 2 - 1, "*"), "\n"] stem = [d(n - 1, " "), "|"] IO.puts [leaves, stem] end defp d(n, s), do: String.duplicate(s, n) end defmodule SinglePrintNoDuplicateIOListTree do def print(n) do leaves = for i <- 1 .. n, do: [d(n - i, " "), d(i * 2 - 1, "*"), "\n"] stem = [d(n - 1, " "), "|"] IO.puts [leaves, stem] end defp d(n, s, a \\ []) defp d(0, _, a), do: a defp d(n, s, a), do: d(n - 1, s, [s | a]) end test = fn -> for x <- 1 .. 10 do {a, _} = :timer.tc(fn -> for _ <- 1 .. 1000, do: Tree.print(x) end) {b, _} = :timer.tc(fn -> for _ <- 1 .. 1000, do: BloatedTree.print(x) end) {c, _} = :timer.tc(fn -> for _ <- 1 .. 1000, do: SinglePrintTree.print(x) end) {d, _} = :timer.tc(fn -> for _ <- 1 .. 1000, do: SinglePrintIOListTree.print(x) end) {e, _} = :timer.tc(fn -> for _ <- 1 .. 1000, do: SinglePrintNoDuplicateIOListTree.print(x) end) {x, a, b, c, d, e} end end ((fn -> # # With I/O to STDIO # # [ # {1, 12529, 9639, 5628, 4913, 5047}, # {2, 40616, 15349, 4672, 5342, 7255}, # {3, 86754, 42744, 13583, 15537, 16900}, # {4, 179915, 23662, 10248, 6174, 9779}, # {5, 199164, 29099, 8647, 6944, 8353}, # {6, 275208, 33379, 9747, 7681, 7818}, # {7, 373047, 40429, 14440, 11855, 11288}, # {8, 532534, 42508, 17121, 31591, 16638}, # {9, 566551, 48979, 11900, 21552, 12687}, # {10, 723348, 55072, 12314, 24258, 14422} # ] # test.() |> IO.inspect end).()) ((fn -> # # Without I/O to STDIO via I/O capturing in ExUnit # # [ # {1, 5053, 4624, 3391, 2768, 2130}, # {2, 14860, 6711, 3154, 2536, 2491}, # {3, 34378, 14339, 6651, 4077, 4985}, # {4, 64317, 14771, 5655, 4150, 5587}, # {5, 151442, 21348, 7600, 5281, 5953}, # {6, 149298, 17912, 6075, 4475, 9791}, # {7, 174312, 18847, 6975, 4240, 5562}, # {8, 210175, 22379, 7177, 4889, 7097}, # {9, 254250, 25438, 9082, 5199, 7628}, # {10, 342061, 56717, 18038, 10416, 19400} # ] # {:ok, result_agent} = Agent.start_link(fn -> nil end) ExUnit.CaptureIO.capture_io(fn -> result = test.() Agent.update(result_agent, fn _ -> result end) end) Agent.get(result_agent, fn x -> x end) |> IO.inspect end).())
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.