Open
Description
Here is what kind of Stream I came to independently, when I was playing with purescript-parsing:
class Stream f c | f -> c where
uncons :: f -> Maybe { head :: c, tail :: f, updatePos :: Position -> Position }
stripPrefix :: Prefix f -> f -> Maybe { rest :: f, updatePos :: Position -> Position }
class HasUpdatePosition a where
updatePos :: Position -> a -> Position
newtype Prefix a = Prefix a
-- example implementation for list
instance (Eq a, HasUpdatePosition a) => Stream (List.List a) a where
uncons f = L.uncons f <#> \({ head, tail}) ->
{ head, tail, updatePos: (_ `updatePos` head)}
stripPrefix (Prefix p) s = List.stripPrefix (List.Pattern p) s <#> \rest ->
{ rest, updatePos: unwrap (fold (p <#> (flip updatePos >>> Endo)))}
purescript-contrib/purescript-parsing#62
I think this formulation is nicer as you don't need to carry updatePosition function around, and I don't see why you need the m
in uncons
too.
Would like your thoughts on this formulation.
Metadata
Metadata
Assignees
Labels
No labels