Skip to content

Instantly share code, notes, and snippets.

@joan38
Last active May 21, 2018 16:12
Show Gist options
  • Select an option

  • Save joan38/7f0a92b77b9d1bb69eb8ce29a30c9052 to your computer and use it in GitHub Desktop.

Select an option

Save joan38/7f0a92b77b9d1bb69eb8ce29a30c9052 to your computer and use it in GitHub Desktop.
postsByAuthor
.join(usersByKey,
(posts: Set[Post], author: User) =>
posts.map(DenormalisedPost(_, author, DenormalisedPost.Interactions(Set.empty, 0))))
.toStream
.flatMapValues(identity)
.groupBy((_, denormalisedPost) => denormalisedPost.post.id)
.reduce((first, second) => if (first.post.updatedOn.isAfter(second.post.updatedOn)) first else second)
.leftJoin(likesByKey,
(denormalisedPost: DenormalisedPost, likes: Set[Like]) =>
Option(likes).fold(denormalisedPost)(denormalisedPost.lens(_.interactions.likes).set(_)))
.leftJoin(commentCountByKey,
(denormalisedPost: DenormalisedPost, commentCount: Int) =>
denormalisedPost.lens(_.interactions.comments).set(commentCount))
.toStream
.to("denormalised-posts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment