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
select :: IRequest req a => req a -> AsyncFetch req a | |
select req = AsyncFetch $ \cacheVar -> do | |
cache <- takeMVar cacheVar | |
case lookupCache req cache of | |
Just var -> do | |
putMVar cacheVar cache | |
pure var | |
Nothing -> do | |
var <- fetch req | |
putMVar cacheVar (insertCache req var cache) |
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
newtype RequestCache req = | |
RequestCache (forall a. HashMap (req a) (MVar a)) | |
emptyCache :: RequestCache req | |
lookupCache :: IRequest req a => req a -> RequestCache req -> Maybe (MVar a) | |
insertCache :: IRequest req a => req a -> MVar a -> RequestCache req -> RequestCache req |
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
class Fetchable req where | |
fetch :: req a -> IO (MVar a) | |
type Cachable req a = (Eq (req a), Hashable (req a)) | |
type IRequest req a = (Fetchable req, Cachable req a) |
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
class Monad m => WithProfileInfo m where | |
friendsOf :: ProfileId -> m [ProfileId] | |
favoriteTopicsOf :: ProfileId -> m (Set Topic) | |
lastPostsOf :: ProfileId -> m [BlogPost] |
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
suggestedPostsFor :: (WithProfileInfo m) => ProfileId -> m [BlogPost] | |
suggestedPostsFor userId = do | |
friendIds <- friendsOf userId | |
topics <- favoriteTopicsOf userId | |
friendsPosts <- forM friendIds $ \friendId -> do | |
posts <- lastPostsOf friendId | |
return (filter (isAbout topics) posts) | |
return (mostLiked 3 (concat friendsPosts)) | |
mostLiked :: Int -> [BlogPost] -> [BlogPost] |
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
algorithm1.suggestedPostsFor(new ProfileId(1)); // First algorithm | |
algorithm2.suggestedPostsFor(new ProfileId(2)); // Second algorithm |
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
algorithm1.suggestedPostsFor(new ProfileId(1)); | |
algorithm1.suggestedPostsFor(new ProfileId(2)); |
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
class SuggestPopularFriendPost { | |
private final IAccessProfileInfo profileInfo; | |
private final ExecutorService executor; // Where would it come from? | |
private Future<Iterable<ProfileId> friendsOf(ProfileId profileId) { | |
return executor.submit(() -> profileInfo.friendsOf(profileId)); | |
} | |
// ... remaining code ... | |
} |
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
template<typename T> | |
struct Node { | |
T content; | |
Node<T>* next; | |
}; |
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 interface IAccessProfileInfo { | |
Future<Map<ProfileId, Iterable<ProfileId>>> friendsOf(Iterable<ProfileId> profileIds); | |
Future<Map<ProfileId, Set<Topic>>> favoriteTopicsOf(Iterable<ProfileId> profileIds); | |
Future<Map<ProfileId, Iterable<PostSummary>>> lastPostsOf(Iterable<ProfileId> profileIds); | |
} |