我最近在 Hackage 上发现了 lens 包,并且现在一直在尝试在一个小型测试项目中使用它,如果我继续努力的话,在很遥远的一天可能会变成 MUD/MUSH 服务器。
这是我的代码的最小化版本,说明了我现在使用用于访问键/值容器的 at 镜头面临的问题(在我的情况下为 Data.Map.Strict)
{-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving, TemplateHaskell #-}
module World where
import Control.Applicative ((<$>),(<*>), pure)
import Control.Lens
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as DM
import Data.Maybe
import Data.UUID
import Data.Text (Text)
import qualified Data.Text as T
import System.Random (Random, randomIO)
newtype RoomId = RoomId UUID deriving (Eq, Ord, Show, Read, Random)
newtype PlayerId = PlayerId UUID deriving (Eq, Ord, Show, Read, Random)
data Room =
Room { _roomId :: RoomId
, _roomName :: Text
, _roomDescription :: Text
, _roomPlayers :: [PlayerId]
} deriving (Eq, Ord, Show, Read)
makeLenses ''Room
data Player =
Player { _playerId :: PlayerId
, _playerDisplayName :: Text
, _playerLocation :: RoomId
} deriving (Eq, Ord, Show, Read)
makeLenses ''Player
data World =
World { _worldRooms :: Map RoomId Room
, _worldPlayers :: Map PlayerId Player
} deriving (Eq, Ord, Show, Read)
makeLenses ''World
mkWorld :: IO World
mkWorld = do
r1 <- Room <$> randomIO <*> (pure "The Singularity") <*> (pure "You are standing in the only place in the whole world") <*> (pure [])
p1 <- Player <$> randomIO <*> (pure "testplayer1") <*> (pure $ r1^.roomId)
let rooms = at (r1^.roomId) ?~ (set roomPlayers [p1^.playerId] r1) $ DM.empty
players = at (p1^.playerId) ?~ p1 $ DM.empty in do
return $ World rooms players
viewPlayerLocation :: World -> PlayerId -> RoomId
viewPlayerLocation world playerId=
view (worldPlayers.at playerId.traverse.playerLocation) world
由于房间、玩家和类似对象在整个代码中都被引用,因此我将它们存储在我的 World 状态类型中,作为其数据对象的 Id(新类型 UUID)映射。
要检索那些有镜头的人,我需要以某种方式处理 at 镜头返回的 Maybe (如果键不在地图中,这就是 Nothing)。在我的最后一行中,我尝试通过 traverse 执行此操作,只要最终结果是 Monoid 的实例,它就会进行类型检查,但通常情况并非如此。这不是因为 playerLocation 返回一个没有 Monoid 实例的 RoomId。
No instance for (Data.Monoid.Monoid RoomId)
arising from a use of `traverse'
Possible fix:
add an instance declaration for (Data.Monoid.Monoid RoomId)
In the first argument of `(.)', namely `traverse'
In the second argument of `(.)', namely `traverse . playerLocation'
In the second argument of `(.)', namely
`at playerId . traverse . playerLocation'
由于 traverse 只需要 Monoid,因为 traverse 泛化到大小大于 1 的容器,我现在想知道是否有更好的方法来处理这个问题,它不需要我想要的对象中可能包含的所有类型的语义上无意义的 Monoid 实例存储在地图中。
或者我可能完全误解了这里的问题,我需要使用一个完全不同的相当大的镜头包?