Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Scala

(GrabbaBeer & CaseOfBeers)

Date:05/30/09
Author:D Mackenzie
URL:n/a
Comments:0
Info:http://www.scala-lang.org
Score: (2.91 in 11 votes)
import io.Source
// Lazy, brittle "rob the store" version
object GrabbaBeer extends Application {
  val buf = new StringBuilder; for (c <-
Source.fromURL("http://99-bottles-of-beer.net/lyrics.html")) buf.append(c)
  val Pat = "<p>(.*?)<br/?>\\s*(.*?)</p>".r

  for (Pat(l1, l2) <- Pat findAllIn buf) println(l1 + "\n" + l2 + "\n")
}

// Law-abiding concise version.
object CaseOfBeers extends Application {
  def qty(n: Int) = (n match {case 0 => "no more bottles" case 1 => "1 bottle" case n => n + "
bottles"}) + " of beer"

  for (b <- 99 to 0 by -1)
    println(qty(b).capitalize + " on the wall, " + qty(b) + "." +
          (if (b > 0) "\nTake one down and pass it around, " + qty(b - 1) + " on the wall.\n"
           else "\nGo to the store and buy more beer, " + qty(99) + " on the wall."))
}

// Cheers!

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Scala with a vengeanceEduardo Costa09/16/090
Fun semi-DSL readable versionCalum Leslie01/31/080
Animated versionD Mackenzie10/02/090

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: