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