在 Haskell 中将 Functor 映射到另一个 Functor 中
Map Functor inside another Functor in Haskell
我应该从 maybeAuth
得到 Maybe UserId
我打算这样做:
... = do
muserId <- (entityKey <$>) <$> maybeAuth
...
因此,我希望将 Functor
映射到另一个 Functor
中。我听说过 Functor 组合,是我的情况吗?
我可以使用 Functor Composition 或其他方法改进它吗?
作为一般方法,您可以使用 Data.Functor.Compose。这是一个组成可能列表的示例:
Prelude Data.Functor.Compose> ff = Compose [Just 42, Just 1337, Nothing, Just 2112]
Prelude Data.Functor.Compose> :t ff
ff :: Num a => Compose [] Maybe a
Prelude Data.Functor.Compose> fmap (show . (+1)) ff
Compose [Just "43",Just "1338",Nothing,Just "2113"]
我应该从 maybeAuth
得到Maybe UserId
我打算这样做:
... = do
muserId <- (entityKey <$>) <$> maybeAuth
...
因此,我希望将 Functor
映射到另一个 Functor
中。我听说过 Functor 组合,是我的情况吗?
我可以使用 Functor Composition 或其他方法改进它吗?
作为一般方法,您可以使用 Data.Functor.Compose。这是一个组成可能列表的示例:
Prelude Data.Functor.Compose> ff = Compose [Just 42, Just 1337, Nothing, Just 2112]
Prelude Data.Functor.Compose> :t ff
ff :: Num a => Compose [] Maybe a
Prelude Data.Functor.Compose> fmap (show . (+1)) ff
Compose [Just "43",Just "1338",Nothing,Just "2113"]