Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active March 29, 2018 09:20
Show Gist options
  • Save deque-blog/71166a7766e62afad6fac70960214647 to your computer and use it in GitHub Desktop.
Save deque-blog/71166a7766e62afad6fac70960214647 to your computer and use it in GitHub Desktop.
public Iterable<PostSummary> suggestedPostsFor(ProfileId profileId) {
Iterable<ProfileId> friendIds = friendsOf(profileId); // Get the friends of the current user
Set<Topic> topics = favoriteTopicsOf(profileId); // Get the favorite topics of the current user
Stream<PostSummary> friendsPosts = stream(friendIds) // For each friend
.flatMap(friendId -> lastPostsOf(friendId)) // * Get their last posts
.filter(post -> post.isAbout(topics)); // * Keep those whose topic is relevant
return mostLiked(3, friendsPosts); // Rank them by like count
}
private List<PostSummary> mostLiked(int count, Stream<PostSummary> posts) {
return posts
.sorted(reverseOrder(comparing(PostSummary::getLikesCount)))
.limit(count)
.collect(Collectors.toList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment