The learning of most programming languages revolve around some key concepts. Conditions (if/then) and loops. Now with loops they come in two forms, do something while something is true (a while loop) and do something for a predetermined number of times (the for loop).
Now I need a good Christmassy excuse to do a demo. The Twelve Days of Christmas is a good one as far as I’m concerned.
As scripts go it’s rather simple, two lists and two for loops.
So the first list deals with the day that my true love gave me something:
var dayname = List("First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Nineth", "Tenth", "Eleventh", "Twelth")
The second list are the items that she decided to give:
var truelove = List("A Partridge in a pear tree", "Two turtle doves and a", "Three French Hens","Four Colly birds","Five Gold Rings","Six Geese-a-laying","Seven Swans-a-Swimming","Eight Maids-a-milking","Nine ladies dancing","Ten Lords-a-leaping","Eleven Pipers piping","Twelve Drummers drumming")
So that’s our data dealt with, just need a way to show it. We know for fact there are twelve days of Christmas (the title of the song is a good indication) so let’s start there:
for(h <- 1 to 12) { println("On the " + dayname(h-1) + " day of Christmas my true love gave to me") for(i <- h to 1 by -1) { println(truelove(i-1)) } println()}
The first for loop deals with the days, the second takes the day and shows the items that my true love gave to me on that day.
So we end up with….
jason-bells-mac:12days jasonbell$ scala 12days.scala
On the First day of Christmas my true love gave to me
A Partridge in a pear tree
On the Second day of Christmas my true love gave to me
Two turtle doves and a
A Partridge in a pear tree
On the Third day of Christmas my true love gave to me
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Fourth day of Christmas my true love gave to me
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Fifth day of Christmas my true love gave to me
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Sixth day of Christmas my true love gave to me
Six Geese-a-laying
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Seventh day of Christmas my true love gave to me
Seven Swans-a-Swimming
Six Geese-a-laying
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Eighth day of Christmas my true love gave to me
Eight Maids-a-milking
Seven Swans-a-Swimming
Six Geese-a-laying
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Nineth day of Christmas my true love gave to me
Nine ladies dancing
Eight Maids-a-milking
Seven Swans-a-Swimming
Six Geese-a-laying
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Tenth day of Christmas my true love gave to me
Ten Lords-a-leaping
Nine ladies dancing
Eight Maids-a-milking
Seven Swans-a-Swimming
Six Geese-a-laying
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Eleventh day of Christmas my true love gave to me
Eleven Pipers piping
Ten Lords-a-leaping
Nine ladies dancing
Eight Maids-a-milking
Seven Swans-a-Swimming
Six Geese-a-laying
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree
On the Twelth day of Christmas my true love gave to me
Twelve Drummers drumming
Eleven Pipers piping
Ten Lords-a-leaping
Nine ladies dancing
Eight Maids-a-milking
Seven Swans-a-Swimming
Six Geese-a-laying
Five Gold Rings
Four Colly birds
Three French Hens
Two turtle doves and a
A Partridge in a pear tree