The spiral program in Scala: Spiral The code together

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.