Created
February 17, 2012 17:27
-
-
Save seanparsons/1854485 to your computer and use it in GitHub Desktop.
Would you rather write something functionally or imperatively?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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