Skip to content

Instantly share code, notes, and snippets.

@jsuereth
Created April 21, 2011 00:45
Show Gist options
  • Save jsuereth/933419 to your computer and use it in GitHub Desktop.
Save jsuereth/933419 to your computer and use it in GitHub Desktop.
For Tim
scala> def replace(what: String)(rules: List[(String,String)]): String =
| rules.foldLeft(what)((seed,tuple) =>
| seed.replaceAll(tuple._1,tuple._2))
replace: (what: String)(rules: List[(String, String)])String
scala> replace("Little Bunny Foo foo")(List("Foo" -> "Pancake", "Bunny" -> "Rabbit"))
res3: String = Little Rabbit Pancake foo
scala> def recurse(times: Int)(what: => String): String = {
| def rpt(times: Int, what: => String, aggregator: String): String = {
| println("Agg: " + aggregator)
| times match {
| case 0 => println("Done"); aggregator
| case _ => println("Recursing"); rpt(times - 1, what, aggregator + what)
| }
| }
| rpt(times, what, "")
| }
recurse: (times: Int)(what: => String)String
scala> resurce(5)("O HAI ")
<console>:8: error: not found: value resurce
resurce(5)("O HAI ")
^
scala> recurse(5)("O HAI ")
Agg:
Recursing
Agg: O HAI
Recursing
Agg: O HAI O HAI
Recursing
Agg: O HAI O HAI O HAI
Recursing
Agg: O HAI O HAI O HAI O HAI
Recursing
Agg: O HAI O HAI O HAI O HAI O HAI
Done
res2: String = "O HAI O HAI O HAI O HAI O HAI "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment