Skip to content

Instantly share code, notes, and snippets.

@seanparsons
Created February 17, 2012 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanparsons/1854485 to your computer and use it in GitHub Desktop.
Save seanparsons/1854485 to your computer and use it in GitHub Desktop.
Would you rather write something functionally or imperatively?
import scalaz._
import Scalaz._
case class Score(offline: Int, online: Int)
val scores = List(Score(100, 200), Score(103, 99), Score(12, 0))
// Written functionally:
val (offlineScores, onlineScores) = scores.foldMap(score => (score.offline, score.online))
// Written imperatively:
var workingOfflineScores = 0
var workingOnlineScores = 0
for (score <- scores) {
workingOfflineScores += score.offline
workingOnlineScores += score.online
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment