Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. defmodule Recursion do
  2. require Integer
  3. # Recursion for star rows
  4. def print_star_row_pair_n_minus_1_times(n) when n <= 1 do
  5. print_row AmericanFlag.star_row(IO.ANSI.white_background)
  6. end
  7.  
  8. def print_star_row_pair_n_minus_1_times(n) do
  9. print_row AmericanFlag.star_row(IO.ANSI.white_background)
  10. print_row AmericanFlag.star_row(IO.ANSI.red_background)
  11. print_star_row_pair_n_minus_1_times(n-1)
  12. end
  13.  
  14. # Recursion for color rows
  15. def print_color_row_n_times(n) when n <= 1 do
  16. print_row AmericanFlag.color_row(IO.ANSI.white_background)
  17. end
  18.  
  19. def print_color_row_n_times(n) do
  20. if Integer.is_even n do
  21. print_row AmericanFlag.color_row(IO.ANSI.red_background)
  22. else
  23. print_row AmericanFlag.color_row(IO.ANSI.white_background)
  24. end
  25. print_color_row_n_times(n-1)
  26. end
  27.  
  28. # Allow printing of a single row N times
  29. def print_row_n_times(msg, n) when n <= 1 do
  30. :ok = IO.write msg <> ansi_reset
  31. end
  32.  
  33. def print_row_n_times(msg, n) do
  34. :ok = IO.write msg <> ansi_reset
  35. print_row_n_times(msg, n-1)
  36. end
  37.  
  38. # Helper Methods
  39. defp ansi_reset do
  40. "#{IO.ANSI.reset}" <> "\n"
  41. end
  42.  
  43. defp print_row(msg) do
  44. :ok = IO.write msg <> ansi_reset
  45. end
  46. end
  47.  
  48. defmodule AmericanFlag do
  49. def star_row(background) do
  50. "#{IO.ANSI.white}#{IO.ANSI.blue_background} * * * * * * " <>
  51. "#{background} "
  52. end
  53.  
  54. def color_row(color) do
  55. "#{color} "
  56. end
  57. end
  58.  
  59. Recursion.print_star_row_pair_n_minus_1_times(5)
  60. Recursion.print_color_row_n_times(6)
  61. Recursion.print_row_n_times("#{IO.ANSI.white}#{IO.ANSI.black_background}| |", 12)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement