twiddleEx = mconcat ((~~) <$> hexagon 1 <*> hexagon 1) # centerXY # pad 1.1
(%@~) ≡ ioverWhen you do not need access to the index then (%~) is more liberal in what it can accept.
l %~ f ≡ l %@~ const f
(%@~) :: IndexedSetter i s t a b -> (i -> a -> b) -> s -> t (%@~) :: IndexedLens i s t a b -> (i -> a -> b) -> s -> t (%@~) :: IndexedTraversal i s t a b -> (i -> a -> b) -> s -> t
fmap f ≡ mapped %~ f fmapDefault f ≡ traverse %~ f
>>> (a,b,c) & _3 %~ f (a,b,f c)
>>> (a,b) & both %~ f (f a,f b)
>>> _2 %~ length $ (1,"hello") (1,5)
>>> traverse %~ f $ [a,b,c] [f a,f b,f c]
>>> traverse %~ even $ [1,2,3] [False,True,False]
>>> traverse.traverse %~ length $ [["hello","world"],["!!!"]] [[5,5],[3]]
(%~) :: Setter s t a b -> (a -> b) -> s -> t (%~) :: Iso s t a b -> (a -> b) -> s -> t (%~) :: Lens s t a b -> (a -> b) -> s -> t (%~) :: Traversal s t a b -> (a -> b) -> s -> t
>>> both &&~ True $ (False, True) (False,True)
>>> both &&~ False $ (False, True) (False,False)
(&&~) :: Setter' s Bool -> Bool -> s -> s (&&~) :: Iso' s Bool -> Bool -> s -> s (&&~) :: Lens' s Bool -> Bool -> s -> s (&&~) :: Traversal' s Bool -> Bool -> s -> s
>>> (10,20) & _1 .~ 30 & _2 .~ 40 (30,40)
>>> (10,20) &~ do _1 .= 30; _2 .= 40 (30,40)This does not support type-changing assignment, e.g.
>>> (10,20) & _1 .~ "hello" ("hello",20)
>>> (a,b) & _1 **~ c (a**c,b)
>>> (a,b) & both **~ c (a**c,b**c)
>>> _2 **~ 10 $ (3,2) (3,1024.0)
(**~) :: Floating a => Setter' s a -> a -> s -> s (**~) :: Floating a => Iso' s a -> a -> s -> s (**~) :: Floating a => Lens' s a -> a -> s -> s (**~) :: Floating a => Traversal' s a -> a -> s -> s
>>> (a,b) & _1 *~ c (a * c,b)
>>> (a,b) & both *~ c (a * c,b * c)
>>> (1,2) & _2 *~ 4 (1,8)
>>> Just 24 & mapped *~ 2 Just 48
(*~) :: Num a => Setter' s a -> a -> s -> s (*~) :: Num a => Iso' s a -> a -> s -> s (*~) :: Num a => Lens' s a -> a -> s -> s (*~) :: Num a => Traversal' s a -> a -> s -> s
>>> (a,b) & _1 +~ c (a + c,b)
>>> (a,b) & both +~ c (a + c,b + c)
>>> (1,2) & _2 +~ 1 (1,3)
>>> [(a,b),(c,d)] & traverse.both +~ e [(a + e,b + e),(c + e,d + e)]
(+~) :: Num a => Setter' s a -> a -> s -> s (+~) :: Num a => Iso' s a -> a -> s -> s (+~) :: Num a => Lens' s a -> a -> s -> s (+~) :: Num a => Traversal' s a -> a -> s -> s
>>> (a,b) & _1 -~ c (a - c,b)
>>> (a,b) & both -~ c (a - c,b - c)
>>> _1 -~ 2 $ (1,2) (-1,2)
>>> mapped.mapped -~ 1 $ [[4,5],[6,7]] [[3,4],[5,6]]
(-~) :: Num a => Setter' s a -> a -> s -> s (-~) :: Num a => Iso' s a -> a -> s -> s (-~) :: Num a => Lens' s a -> a -> s -> s (-~) :: Num a => Traversal' s a -> a -> s -> s
(.@~) ≡ isetWhen you do not need access to the index then (.~) is more liberal in what it can accept.
l .~ b ≡ l .@~ const b
(.@~) :: IndexedSetter i s t a b -> (i -> b) -> s -> t (.@~) :: IndexedLens i s t a b -> (i -> b) -> s -> t (.@~) :: IndexedTraversal i s t a b -> (i -> b) -> s -> t
f <$ a ≡ mapped .~ f $ a
>>> (a,b,c,d) & _4 .~ e (a,b,c,e)
>>> (42,"world") & _1 .~ "hello" ("hello","world")
>>> (a,b) & both .~ c (c,c)
(.~) :: Setter s t a b -> b -> s -> t (.~) :: Iso s t a b -> b -> s -> t (.~) :: Lens s t a b -> b -> s -> t (.~) :: Traversal s t a b -> b -> s -> t
>>> (a,b) & _1 //~ c (a / c,b)
>>> (a,b) & both //~ c (a / c,b / c)
>>> ("Hawaii",10) & _2 //~ 2 ("Hawaii",5.0)
(//~) :: Fractional a => Setter' s a -> a -> s -> s (//~) :: Fractional a => Iso' s a -> a -> s -> s (//~) :: Fractional a => Lens' s a -> a -> s -> s (//~) :: Fractional a => Traversal' s a -> a -> s -> s
l <%~ f ≡ l <%@~ const fWhen you do not need access to the index then (<%~) is more liberal in what it can accept. If you do not need the intermediate result, you can use (%@~) or even (%~).
(<%@~) :: IndexedLens i s t a b -> (i -> a -> b) -> s -> (b, t) (<%@~) :: Monoid b => IndexedTraversal i s t a b -> (i -> a -> b) -> s -> (b, t)