Last active
March 29, 2018 09:20
-
-
Save deque-blog/71166a7766e62afad6fac70960214647 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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