Created
January 10, 2015 22:24
-
-
Save EncodePanda/5819277e4c2de6689268 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Given the following file, create a simple Scala programming example | |
* that would parse and then randomly output a quote. | |
* (http://www.coverfire.com/files/quotes.txt) | |
*/ | |
import scala.io.Source | |
import scala.util.Random | |
case class QuotesForgery(quotes: List[String] = List(), val workspace: List[String] = List()) { | |
private val random = new Random | |
def forgeNewQuote = new QuotesForgery(workspace.reverse.mkString("\n")::quotes, List()) | |
def pickUpRandom = quotes(random.nextInt(quotes.length)) | |
} | |
val sourceURL: String = "http://www.coverfire.com/files/quotes.txt" | |
val source = Source.fromURL(sourceURL) | |
val quotesForgery = source.getLines.foldLeft(new QuotesForgery){ | |
case (qh: QuotesForgery, "") => qh.forgeNewQuote | |
case (QuotesForgery(q, w), line: String) => new QuotesForgery(q, line::w) | |
} | |
val randomeQuote = quotesForgery.pickUpRandom |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment