Last active
December 19, 2015 13:19
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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