Skip to content

Instantly share code, notes, and snippets.

@seanparsons
Created April 12, 2013 22:09
Show Gist options
  • Save seanparsons/5375556 to your computer and use it in GitHub Desktop.
Save seanparsons/5375556 to your computer and use it in GitHub Desktop.
Loops? No Thanks!
// This bit is mostly getting imports together and some simple types.
import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
import scalaz._,Scalaz._
case class User(id: Int, name: String, score: Long)
// EXAMPLE: Turn collection into another collection.
// With a for loop (in this case a foreach):
val original = List("sean", "brett")
val listBuffer = ListBuffer[String]()
for (name <- original) listBuffer += name.toUpperCase
val newList = listBuffer.toList
// Without a loop:
val original = List("sean", "brett")
val newList = original.map(name => name.toUpperCase)
// EXAMPLE: Filter items from a collection.
// With a for loop:
def predicate(n: Int): Boolean = n < 50
val original = Vector(1, 10, 100, 1000)
var newVector = Vector.empty[Int]
for (i <- 0 to original.size - 1) {
val value = original(i)
if (predicate(value)) {
newVector = newVector :+ value
}
}
println(newVector)
// Without a loop:
def predicate(n: Int): Boolean = n < 50
val original = Vector(1, 10, 100, 1000)
val newVector = original.filter(predicate)
println(newVector)
// EXAMPLE: Perform some side effect for every element in a collection.
// With a for loop:
def printOut(text: String) = println("OUTPUT: " + text)
val collection = Vector("sean", "brett")
for (i <- 0 to collection.size - 1) {
printOut(collection(i))
}
// Without a for loop:
def printOut(text: String) = println("OUTPUT: " + text)
val collection = Vector("sean", "brett")
collection.foreach(printOut)
// EXAMPLE: Total up the scores for a bunch of users.
// With a for loop:
val users = Vector(User(1, "Sean", 2000), User(2, "Brett", 500))
var score: Long = 0
for (i <- 0 to users.size - 1) {
score += users(i).score
}
println("Score is " + score)
// Without a for loop:
val users = Vector(User(1, "Sean", 2000), User(2, "Brett", 500))
val score = users.foldMap(user => user.score)
println("Score is " + score)
// EXAMPLE: Print out the first user that has a score greater than 1000, if there is any.
// With a while loop:
val users = Vector(User(2, "Brett", 500), User(1, "Sean", 2000))
var found: Boolean = false
var i: Int = 0
while(i <= users.size - 1 && !found) {
val user = users(i)
if (user.score > 1000) {
found = true
println("The first person with a score greater than 1000 is: " + user.name)
} else {
i += 1
}
}
// Without a for loop:
val users = List(User(2, "Brett", 500), User(1, "Sean", 2000))
@tailrec // Compiler checks we're tail recursive with this.
def printoutFirstBigScore(userList: List[User]) {
userList match {
case user :: _ if (user.score > 1000) => {
println("The first person with a score greater than 1000 is: " + user.name)
}
case _ :: tail => printoutFirstBigScore(tail)
case _ => ()
}
}
printoutFirstBigScore(users)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment