Skip to content

Instantly share code, notes, and snippets.

@mitallast
Last active August 17, 2016 14:11
Show Gist options
  • Save mitallast/2209f19c5c7c5583a8aa1041a13bd9c6 to your computer and use it in GitHub Desktop.
Save mitallast/2209f19c5c7c5583a8aa1041a13bd9c6 to your computer and use it in GitHub Desktop.
case class Semigroup(x: Map[Long, Long]) {
// {1->1, 2->2} |+| {1->1, 2->4, 3->3} == {1->2, 2->6, 3->3}
def |+|(y: Map[Long, Long]): Map[Long, Long] = x.keys.toSet.union(y.keys.toSet)
.map { k => k -> (x.getOrDefault(k, 0) + y.getOrDefault(k, 0)) }.toMap
// {1->1, 2->2} |*| 2 == {1->2, 2->4}
def |*|(y: Int): Map[Long, Long] = x.map { case (k, v) ⇒ k → v * y }
}
implicit def map2Semigroup(x: Map[Long, Long]): Semigroup = Semigroup(x)
def engagement(liked: Map[Long, Long], reposted: Map[Long, Long], commented: Map[Long, Long]): Map[Long, Long] = {
liked |+| (reposted |*| 6) |+| (commented |*| 9)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment