On BNET: 5 classic computer pranks
BNET Business Network:
BNET
TechRepublic
ZDNet
TalkBack 1 of 1:
An alternative implementation
David,

I really like your code. I've got an alternative implementation that demonstrates a slightly different take on Scala:

def interpret(in: String, f: String => Unit) {

def sing(n: Int): List[String] = {
def bb(in: Int) = in match {
case 1 => "1 bottle of beer"
case x => ""+x+" bottles of beer"
}

if (n == 0) Nil
else {
f(bb(n)+" on the wall.")
f(bb(n)+".")
f("Take one down, pass it around,")
f(bb(n - 1)+" on the wall.")
sing(n - 1)
}
}

in.map(Character.toUpperCase).
foldLeft(0) {
case (x, 'H') => f("Hello World") ; x
case (x, 'Q') => f(in) ; x
case (x, '9') => sing(99) ; x
case (x, '+') => x + 1
case (x, _) => x
}
}

interpret(argv(0), println)

I hope the readers like to see the flexibility and different styles in Scala code.

Thanks,

David
Posted by: feeder_of_the_bears   Posted on: 12/08/08 You are currently: a Guest | Members login | Terms of Use

Alert moderator to an offensive message

Subscribe to this discussion via Email or RSS

An alternative implementation  feeder_of_the_bears | 12/08/08

What do you think?

SponsoredWhite Papers, Webcasts, and Downloads

advertisement
advertisement

SmartPlanet

Click Here