Skip to content

Instantly share code, notes, and snippets.

@davidandrzej
Last active August 29, 2015 14:04
Show Gist options
  • Save davidandrzej/8ba856345509f6fd8bf3 to your computer and use it in GitHub Desktop.
Save davidandrzej/8ba856345509f6fd8bf3 to your computer and use it in GitHub Desktop.
def addItUp[F : Monoid](items: Seq[F]): F = {
// Combine a bunch of items
val m = implicitly[Monoid[F]]
items.foldLeft(m.zero){case (total, next) => m.append(total,next)}
}
scala> addItUp(Seq("day ", "after ", "day"))
res1: String = "day after day"
scala> addItUp(Seq(1,2,3))
res2: Int = 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment