Существует слой абстракции, используемый Parsec, type class Stream
, он выглядит следующим образом:
class (Monad m, ShowToken t) => Stream s m t | s -> t where
uncons :: s -> m (Maybe (t, s))
instance (Monad m, ShowToken t) => Stream [t] m t where
uncons [] = return Nothing
uncons (t:ts) = return $ Just (t, ts)
{-# INLINE uncons #-}
instance Monad m => Stream CL.ByteString m Char where
uncons = return . CL.uncons
instance Monad m => Stream C.ByteString m Char where
uncons = return . C.uncons
instance Monad m => Stream T.Text m Char where
uncons = return . T.uncons
{-# INLINE uncons #-}
instance Monad m => Stream TL.Text m Char where
uncons = return . TL.uncons
{-# INLINE uncons #-}
Интересно, здесь хорошая идея, почему почему uncons
в ByteString
экземпляре Stream
не указано?
Должны ли все эти функции быть встроенными или ни одна из них, или Text
и ByteString
не настолько различны, что мы должны вставить один и не должны встраивать другую?