Skip to content

Instantly share code, notes, and snippets.

@8th-Light-Blog
Created June 28, 2011 18:00
Show Gist options
  • Save 8th-Light-Blog/1051734 to your computer and use it in GitHub Desktop.
Save 8th-Light-Blog/1051734 to your computer and use it in GitHub Desktop.
Blog Title: A Functional Refactoring in Scala
Author: Colin Jones
Date: June 16th, 2009
def printUpTo(limit: Int): Unit =
{
var i = 0
while(i <= limit)
{
println("i = " + i)
i += 1
}
}
void printUpTo(int limit)
{
for(int i = 0; i <= limit, i++)
{
System.out.println("i = " + i)
}
}
def printUpTo(limit: Int): Unit = {
for(i <- (0 to limit)) {
println("i = " + i)
}
}
def printUpTo(limit: Int): Unit = {
(0 to limit).foreach {
i => println("i = " + i)
}
}
def printUpTo(limit: Int): Unit = {
applyUpTo(limit, println)
}
def applyUpTo(limit: Int, action: String => Unit): Unit = {
(0 to limit).foreach {
i => action("i = " + i)
}
}
def printUpTo(limit: Int): Unit = {
buildUpTo(limit).foreach(println)
}
def buildUpTo(limit: Int): Iterable[String] = {
(0 to limit).map("i = " + _)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment