Skip to content

Instantly share code, notes, and snippets.

class SuggestPopularFriendPost {
private final IAccessProfileInfo profileInfo;
public SuggestPopularFriendPost(IAccessProfileInfo profileInfo) {
this.profileInfo = profileInfo;
}
public Iterable<PostSummary> suggestedPostsFor(ProfileId profileId) {
Iterable<ProfileId> friendIds = friendsOf(profileId);
Set<Topic> topics = favoriteTopicsOf(profileId);
public Iterable<PostSummary> suggestedPostsFor(ProfileId profileId) {
Iterable<ProfileId> friendIds = friendsOf(profileId); // Get the friends of the current user
// ...
}
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) {
suggestedPostsFor userId = do
friendIds <- friendsOf userId -- Get the friends of the current user
topics <- favoriteTopicsOf userId -- Get the favorite topics of the current user
friendsPosts <- forM friendIds $ \friendId -> do -- For each friend
posts <- lastPostsOf friendId -- * Get their last posts
return (filter (isAbout topics) posts) -- * Keep those whose topic is relevant
return (mostLiked 3 friendsPosts) -- Rank them to keep only the bests
void f (Context const& context, ...);
void caller_from_rest(Context const& context)
{
WithContext restContext("REST", "/api/v1.0/f");
f(context, ...);
// ... "REST" automatically removed at the end
}
void f (Context const& context, ...) {
logger.info(context, "Doing stuff in function f");
}
void caller_from_rest(Context const& context) {
context.put("REST", "/api/v1.0/f");
// ...
f(context, ...);
// ...
context.remove("REST");
void f(Context const& context) {
// ...
logger.info(context, "log message");
// ...
}
class Context {
public:
void put(key const&, json const&);
void remove(key const&);
std::map<key, json> getContext();
//...
};
{-# LANGUAGE FlexibleInstances #-}
module LogContext (testLogContext) where
import Control.Concurrent
-- import Control.Concurrent.Async
--------------------------------------------------------------------------------
-- Abstract logger language
--------------------------------------------------------------------------------
;; Calling make-payment using named arguments
(make-payment
{:money "10 EUR"
:from "Alice"
:to "Bob"})
;; Versus calling make-payment with positional arguments
(make-payment "10 EUR" "Alice" "Bob")