mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	more and better samples for Nu, Racket, Scala
- 99 bottles of beer is more substantial than hello world - also fixed chmod 755 on several .script! files
This commit is contained in:
		
							
								
								
									
										40
									
								
								samples/Scala/99-bottles-of-beer.script!
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										40
									
								
								samples/Scala/99-bottles-of-beer.script!
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| #!/bin/sh | ||||
| exec scala "$0" "$@" | ||||
| !# | ||||
|  | ||||
| object Beers extends Application { | ||||
|  | ||||
| 	def bottles(qty : Int, f : => String) = // higher-order functions | ||||
| 		qty match { | ||||
| 			case 0 => "no more bottles of beer" + f | ||||
| 			case 1 => "1 bottle of beer" + f | ||||
| 			case x => x + " bottles of beer" + f | ||||
| 		} | ||||
|  | ||||
| 	def beers(qty : Int) = bottles(qty, " on the wall.") | ||||
|  | ||||
| 	def sing(qty : Int)(implicit song : String) : String = { | ||||
| 		def takeOne = | ||||
| 			qty match { | ||||
| 				case 0 => "Go to the store and buy some more." | ||||
| 				case x => "Take one down and pass it around." | ||||
| 			} | ||||
|  | ||||
| 		def nextQty = // nested functions | ||||
| 			if (qty == 0) 99 | ||||
| 			else qty - 1 | ||||
|  | ||||
| 		def refrain = { | ||||
| 			beers(qty).capitalize + " " + bottles(qty, "") + ".\n" + | ||||
| 			takeOne + " " + beers(nextQty) + "\n\n" | ||||
| 		} | ||||
|  | ||||
| 		if (qty == -1) song | ||||
| 		else sing(qty - 1)(song + refrain) // tail recursion | ||||
| 	} | ||||
|  | ||||
| 	implicit val headOfSong : String = "" | ||||
|  | ||||
| 	println(sing(99)) // implicit parameter | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user