Skip to content

Instantly share code, notes, and snippets.

@EncodePanda
Created January 10, 2015 22:24
Show Gist options
  • Save EncodePanda/5819277e4c2de6689268 to your computer and use it in GitHub Desktop.
Save EncodePanda/5819277e4c2de6689268 to your computer and use it in GitHub Desktop.
/**
* 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