I wonder if it possible to construct following function
ix :: (Applicative a, Traversable t) => Int -> (v -> a v) -> (t v -> a (t v))
Which uses pure
for all elements except i-th for which v -> a v
is used (Traversal over value with specified index).
Basically I am trying to generalize following function for all traversables. Or is it impossible? Traversable can always be converted to Zipper and I thought it could be generalized.
idx _ _ [] = pure []
idx 0 f (x:xs) = (:xs) <$> f x
idx i f (x:xs) = (x:) <$> idx (i - 1) f xs
Here's an ad-hoc attempt. I'm sure there are more elegant options: