The fun way to drill all these elements of a layout library is to write a procedure that draws a spiral of a given number of boundaries. This spiral program, shown in code 10.14, does this:
Import Element.elem
Object Spiral {
val space = Elem ("")
val corner = elem ("+")
def Spiral (nEd Ges:int, direction:int): Element = {
if (nedges = = 1)
elem ("+")
else {
val sp = Spiral (NEdges-1, (direction + 3)% 4)
def verticalbar = elem (' | ', 1, sp.height)
def horizontalbar = Elem ('-', sp.width, 1)
if (direction = 0)
(corner beside Horizontalbar) above (sp beside space)
else if (direction = 1)
(SP Abov E space) beside (corner above Verticalbar)
else if (direction = 2)
(space beside SP) above (Horizont Albar beside corner)
else
(Verticalbar above corner) beside (space above sp)
}
}
def main (args:array[string]) {
val nsides = args (0). ToInt
println (Spiral (nsides, 0))
}
}
Code 10.14 Spiral Program
Because Spiral is a standalone object with the appropriate signature main method, it is a Scala program. Spiral takes a command-line argument, an integer, and draws a helix with a specific number of bounds. For example, you can draw a six-border spiral, or a larger spiral on the right, as shown on the left side: (slightly)