gpt4 book ai didi

haskell - 如何在不使用求和类型或副本的情况下创建异构数据类型列表

转载 作者:行者123 更新时间:2023-12-02 07:25:50 27 4
gpt4 key购买 nike

用例:一款跨多个消息服务跟踪用户的应用。有一个 TwitterAccount 数据类型,一个 FacebookAccount 数据类型,等等。这些可以很容易地与 Account 总和类型结合在一起,但是下一层层次结构导致了我的问题。

一个 TwitterAccount 有一个 TwitterPost 的列表,一个 FacebookAccount 有一个 FacebookPost 的列表,等等.

我的任务:我希望能够将所有帐户最近 10 天的所有帖子放入一个列表中,并从中提取公共(public)时间和消息正文字段以进行显示。

我失败的方法: 我认为如果 Post 的每个类都实现一个类型类,如 SimplePost 公开函数 messageBodymessageTime 这可能会解决我的问题,但我无法创建 [SimpleMessage] 的列表。

我想保持 TwitterAccount 只能包含TwitterPost 等不变性,所以我不能使用 sum -类型。我不想创建对象的副本来执行此操作。

这个问题最好、最干净、最像 Haskell 的设计是什么?

更新这不是答案,但作为 recursion.ninja 和 Helder Pereira 提供的四种解决方案的替代方案,我一直在考虑是否可以使用幻像类型满足我的不变量,AccountPost 类型包含所有提供者所需的所有可能信息。然而,元组的使用和笨拙的逻辑意味着它不能很好地扩展;也许这应该在不同的问题中。

{-# LANGUAGE EmptyDataDecls #-}   

-- Some FSharpisms
(|>) = flip ($)
(<|) = ($)
infixr 0 <|


data Twitter
data Facebook
data LinkedIn

data Post a = Post{
postBody :: String,
postDate :: UTCTime,
postForwarded :: Bool,
postFriendMentions :: [UserName]
} deriving (Show, Eq)

data Account a = Account {
accountName :: String,
accountPosts :: [Post a]
} deriving (Show, Eq)

data User = User {
userName :: String,
userTweets :: Account Twitter,
userFaces :: Account Facebook,
userLinks :: Account LinkedIn
}

prettyShowUtc :: UTCTime -> String
prettyShowUtc utc = ...

prettyShow :: Post a -> String
prettyShow p = prettyShowUtc (postDate p) ++ " : " ++ show (postBody p)

showOrderedOf2 :: ([Post a], [Post b]) -> [String]
showOrderedOf2 ([], []) = []
showOrderedOf2 (ls, []) = map prettyShow ls
showOrderedOf2 ([], rs) = map prettyShow rs
showOrderedOf2 ((l:ls), (r:rs)) =
if postDate l < postDate r
then prettyShow l : showOrderedOf2 (ls, (r:rs))
else prettyShow r : showOrderedOf2 ((l:ls), rs)

showOrderedOf3 :: ([Post a], [Post b], [Post c]) -> [String]
showOrderedOf3 ([], [], []) = []
showOrderedOf3 (as, [], []) = map postBody as
showOrderedOf3 ([], bs, []) = map postBody bs
showOrderedOf3 ([], [], cs) = map postBody cs
showOrderedOf3 (as, bs, []) = showOrderedOf2 (as, bs)
showOrderedOf3 ([], bs, cs) = showOrderedOf2 (bs, cs)
showOrderedOf3 (as, [], cs) = showOrderedOf2 (as, cs)
showOrderedOf3 ((a:as), (b:bs), (c:cs)) =
let (adate, bdate, cdate) = (postDate a, postDate b, postDate c)
minDate = minimum [adate, bdate, cdate]
in
if adate == minDate
then prettyShow a : showOrderedOf3 (as, (b:bs), (c:cs))
else (if bdate == minDate
then prettyShow b : showOrderedOf3 ((a:as), bs, (c:cs))
else prettyShow c : showOrderedOf3 ((a:as), (b:bs), cs))

createAndShowSample :: IO ()
createAndShowSample =
let faceAc = Account {...} :: Account Facebook
twitAc = Account {...} :: Account Twitter
linkAc = Account {...} :: Account LinkedIn
in
showOrderedOf3 (accountPosts faceAc, accountPosts twitAc, accountPosts linkAc)
|> intercalate "\n"
|> putStrLn

最佳答案

您应该将 FaceBookAccountTwitterAccount 抽象为 SocialMediaAccount 的实例

Haskell 代码:

import Control.Applicative ((<$>))
import Data.List
import Data.Ord
import Data.Time

data FaceBookAccount = FaceBookAccount [FaceBookPost]
data TwitterAccount = TwitterAccount [TwitterPost]
data FaceBookPost = FaceBookPost String UTCTime
data TwitterPost = TwitterPost String UTCTime

data SocialMediaAccount
= SocialMediaAccount
{ accountPosts :: [SocialMediaPost]
}
data SocialMediaPost
= SocialMediaPost
{ postBody :: String
, postTime :: UTCTime
}

class SocialMedia a where
simpleAccount :: a -> SocialMediaAccount

instance SocialMedia FaceBookAccount where
simpleAccount (FaceBookAccount xs) = SocialMediaAccount $ f <$> xs
where
f (FaceBookPost text time) = SocialMediaPost text time

instance SocialMedia TwitterAccount where
simpleAccount (TwitterAccount xs) = SocialMediaAccount $ f <$> xs
where
f (TwitterPost text time) = SocialMediaPost text time

getAllMessages :: (SocialMedia a, SocialMedia b) => a -> b -> [SocialMediaPost]
getAllMessages xs ys = sortBy (comparing postTime)
$ extract xs
++ extract ys
where
extract :: SocialMedia a => a -> [SocialMediaPost]
extract = accountPosts . simpleAccount

关于haskell - 如何在不使用求和类型或副本的情况下创建异构数据类型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32035857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com