haskell - In the lets-lens tutorial, how do you refactor out the call to traverse in order to implement over? -
in the exercises have implemented fmapt
:
-- let's remind ourselves of traversable, noting foldable , functor. -- -- class (foldable t, functor t) => traversable t -- traverse :: -- applicative f => -- (a -> f b) -- -> t -- -> f (t b) -- | observe @fmap@ can recovered @traverse@ using @identity@. -- -- /reminder:/ fmap :: functor t => (a -> b) -> t -> t b fmapt :: traversable t => (a -> b) -> t -> t b fmapt = error "todo: fmapt"
now how implement over
?
-- | let's refactor out call @traverse@ argument @fmapt@. on :: ((a -> identity b) -> s -> identity t) -> (a -> b) -> s -> t on = error "undefined"
you can implement fmapt
using traverse
as:
fmapt f s = runidentity (traverse (identity . f) s)
now next exercise refactor function providing traverse
parameter rather hard-coding in definition. if choose identity
applicative type constructor type of traverse
is:
(traversable t) => (a -> identity b) -> t -> identity (t b)
if provide parameter fmapt
end over
:
over' :: traversable t => ((a -> identity b) -> t -> identity (t b)) -> (a -> b) -> t -> t b over' l f s = runidentity (l (identity . f) s)
since traversable
constraint exists in traverse
not required over'
has more general type of over
given in exercise i.e.
over :: ((a -> identity b) -> (s -> identity t)) -> (a -> b) -> s -> t
and
over' = on traverse
Comments
Post a Comment