Skip to content

Instantly share code, notes, and snippets.

@channingwalton
Last active December 19, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save channingwalton/5961118 to your computer and use it in GitHub Desktop.
Save channingwalton/5961118 to your computer and use it in GitHub Desktop.
An implementation of creating a Christmas tree. The idea was to try to create a solution by composing reusable functions as an alternative to the example here http://sortega.github.io/programming/2013/07/06/functional-tree/
object Trees extends App {
def zero = '0'
def repeat(c: Char)(n: Int) = c.toString * n
def reflect(centre: Char)(s: String) = s.reverse + centre + s
def pad(w: Int, c: Char)(s: String) = s.padTo(w - 1, c)
def line(w: Int, centre: Char)(n: Int) = repeat(zero) _ andThen pad(w, ' ') _ andThen reflect(centre) _ apply (n)
def star(w: Int) = Seq(line(w, '*')(0))
def apply(w: Int): Seq[String] = star(w) ++ Seq.tabulate(w)(line(w, zero))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment