traversable

What should a “higher order Traversable” class look like?

大憨熊 提交于 2019-12-01 17:46:38
问题 In this answer I made up on the spot something which looks a bit like a "higher order Traversable ": like Traversable but for functors from the category of endofunctors on Hask to Hask. {-# LANGUAGE RankNTypes #-} import Data.Functor.Compose import Data.Functor.Identity class HFunctor t where hmap :: (forall x. f x -> g x) -> t f -> t g class HFunctor t => HTraversable t where htraverse :: Applicative g => (forall x. f x -> g x) -> t f -> g (t Identity) htraverse eta = hsequence . hmap eta

What's the most standard/generic way to zip a traversable with a list?

*爱你&永不变心* 提交于 2019-11-28 12:27:14
Traversable is in a sense the class of containers whose structure has a “path” (that can correspond to a list), the elements on which can be modified without dissolving the structure. Hence zipTrav :: Traversable t => t a -> [b] -> Maybe (t (a,b)) zipTrav = evalStateT . traverse zp where zp a = do bs <- get case bs of [] -> lift Nothing (b:bs') -> put bs' >> return (a,b) However, that list-state traversal seems a bit hackish and likely not the most efficient way to do it. I'd suppose there would be a standard function that accomplished the above or a more general task, but I can't figure out

What's the most standard/generic way to zip a traversable with a list?

天涯浪子 提交于 2019-11-27 07:03:38
问题 Traversable is in a sense the class of containers whose structure has a “path” (that can correspond to a list), the elements on which can be modified without dissolving the structure. Hence zipTrav :: Traversable t => t a -> [b] -> Maybe (t (a,b)) zipTrav = evalStateT . traverse zp where zp a = do bs <- get case bs of [] -> lift Nothing (b:bs') -> put bs' >> return (a,b) However, that list-state traversal seems a bit hackish and likely not the most efficient way to do it. I'd suppose there