如何 "wrap" monadic return 值
How to "wrap" monadic return value
我有以下代码:
data APNSIdentifier = NoIdentifier | Identifier Word32 deriving (Show, Eq)
newtype APNSItem = Item Put
createNotificationIdentifierItem :: APNSIdentifier -> APNSItem <--- here
createNotificationIdentifierItem (Identifier identifier) = do
putWord8 3
putWord16be 4
putWord32be identifier
如何 "wrap" Put
带有 APNSItem
的单子?我必须使 APNSItem
成为 Monad
类型类的实例还是有更简单的解决方案?
createNotificationIdentifierItem :: APNSIdentifier -> APNSItem
createNotificationIdentifierItem (Identifier identifier) = Item $ do
putWord8 3
putWord16be 4
putWord32be identifier
或
createNotificationIdentifierItem :: APNSIdentifier -> APNSItem
createNotificationIdentifierItem (Identifier identifier) = do
Item $ putWord8 3
Item $ putWord16be 4
Item $ putWord32be identifier
在使 APNSItem
成为 Monad
的实例之后(您可以使用 GeneralizedNewtypeDeriving
执行此操作,但您需要修复 APNSItem
以具有单一类型变量第一)
我有以下代码:
data APNSIdentifier = NoIdentifier | Identifier Word32 deriving (Show, Eq)
newtype APNSItem = Item Put
createNotificationIdentifierItem :: APNSIdentifier -> APNSItem <--- here
createNotificationIdentifierItem (Identifier identifier) = do
putWord8 3
putWord16be 4
putWord32be identifier
如何 "wrap" Put
带有 APNSItem
的单子?我必须使 APNSItem
成为 Monad
类型类的实例还是有更简单的解决方案?
createNotificationIdentifierItem :: APNSIdentifier -> APNSItem
createNotificationIdentifierItem (Identifier identifier) = Item $ do
putWord8 3
putWord16be 4
putWord32be identifier
或
createNotificationIdentifierItem :: APNSIdentifier -> APNSItem
createNotificationIdentifierItem (Identifier identifier) = do
Item $ putWord8 3
Item $ putWord16be 4
Item $ putWord32be identifier
在使 APNSItem
成为 Monad
的实例之后(您可以使用 GeneralizedNewtypeDeriving
执行此操作,但您需要修复 APNSItem
以具有单一类型变量第一)