One way is to make Iso and compose with it. In ghci:
> import Control.Lens > import Control.Zipper > > data A t = A t > let _A = iso A (\(A a) -> a) > > let a = within (ix 1 . _A) $ zipper ([1,2,3] :: [Int]) > :ta a :: MonadPlus m => m (Zipper Top Int [Int] :>> A Int) > a ^? _Just . focus Just (A 2)
Edit: the reason you need (\(A a) -> a) means you can come back.
> data A t = A t > let _A = iso A (error "Can't unA") > > let a = within (ix 1 . _A) $ zipper ([1,2,3] :: [Int]) > a ^? _Just . focus Just (A 2) > fmap upward a ^? _Just . focus Just [1,*** Exception: Can't unA
I don't think there is a valid way to do this without a function to extract A You can write an invalid Traversal , but it still wonβt work properly:
> data A t = A t > let _A fa = a <$ f (A a) > > let a = within (ix 1 . _A) $ zipper ([1,2,3] :: [Int]) > let b = a & _Just . focus .~ A 10 > b ^? _Just . focus Just (A 10) > fmap upward b ^? _Just . focus Just [1,2,3] -- Should be Just [1, 10, 3]
source share