Skip to content

Commit c34b3bb

Browse files
Rufflewindpaf31
authored andcommitted
Add iforOf{,_}, itraversed, reindexed (#66)
1 parent ba11011 commit c34b3bb

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/Data/Lens/Fold.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,12 @@ itraverseOf_
268268
-> s
269269
-> f Unit
270270
itraverseOf_ p f = ifoldrOf p (\i a fu -> void (f i a) *> fu) (pure unit)
271+
272+
-- | Flipped version of `itraverseOf_`.
273+
iforOf_
274+
:: forall i f s t a b r. (Applicative f)
275+
=> IndexedFold (Endo (f Unit)) i s t a b
276+
-> s
277+
-> (i -> a -> f r)
278+
-> f Unit
279+
iforOf_ = flip <<< itraverseOf_

src/Data/Lens/Indexed.purs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import Prelude
55
import Control.Monad.State (modify, get, evalState)
66

77
import Data.Functor.Compose (Compose(..))
8+
import Data.Lens.Iso.Newtype (_Newtype)
9+
import Data.Lens.Setter ((%~))
810
import Data.Lens.Types (wander, Optic, IndexedOptic, Indexed(..), Traversal, IndexedTraversal)
911
import Data.Newtype (unwrap)
10-
import Data.Profunctor (class Profunctor, dimap)
12+
import Data.Profunctor (class Profunctor, dimap, lmap)
1113
import Data.Profunctor.Star (Star(..))
14+
import Data.Profunctor.Strong (first)
15+
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
1216
import Data.Tuple (curry, fst, snd)
1317

1418
-- | Converts an `IndexedOptic` to an `Optic` by forgetting indices.
@@ -26,13 +30,25 @@ asIndex
2630
-> Optic p s t i b
2731
asIndex l = l <<< Indexed <<< dimap fst id
2832

33+
-- | Remap the index.
34+
reindexed :: forall p i j r a b . Profunctor p =>
35+
(i -> j) -> (Indexed p i a b -> r) -> Indexed p j a b -> r
36+
reindexed ij = (_ <<< _Newtype %~ lmap (first ij))
37+
2938
-- | Converts a `lens`-like indexed traversal to an `IndexedTraversal`.
3039
iwander
3140
:: forall i s t a b
3241
. (forall f. Applicative f => (i -> a -> f b) -> s -> f t)
3342
-> IndexedTraversal i s t a b
3443
iwander itr = wander (\f s -> itr (curry f) s) <<< unwrap
3544

45+
-- | Traverses over a `TraversableWithIndex` container.
46+
itraversed
47+
:: forall i t a b
48+
. TraversableWithIndex i t
49+
=> IndexedTraversal i (t a) (t b) a b
50+
itraversed = iwander traverseWithIndex
51+
3652
-- | Converts a `Traversal` to an `IndexedTraversal` by using the integer
3753
-- | positions as indices.
3854
positions

src/Data/Lens/Traversal.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,13 @@ itraverseOf
8484
-> s
8585
-> f t
8686
itraverseOf t = under Star (t <<< Indexed) <<< uncurry
87+
88+
-- | Flipped version of `itraverseOf`.
89+
iforOf
90+
:: forall f i s t a b
91+
. Applicative f
92+
=> IndexedOptic (Star f) i s t a b
93+
-> s
94+
-> (i -> a -> f b)
95+
-> f t
96+
iforOf = flip <<< itraverseOf

test/Main.purs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import Data.Lens.Fold ((^?))
1111
import Data.Lens.Fold.Partial ((^?!), (^@?!))
1212
import Data.Lens.Grate (Grate, cloneGrate, grate, zipWithOf)
1313
import Data.Lens.Index (ix)
14+
import Data.Lens.Indexed (itraversed, reindexed)
1415
import Data.Lens.Lens (ilens, IndexedLens, cloneIndexedLens)
1516
import Data.Lens.Record (prop)
1617
import Data.Lens.Setter (iover)
17-
import Data.Lens.Zoom (Traversal, Traversal', Lens, Lens', zoom)
18+
import Data.Lens.Zoom (IndexedTraversal', Traversal, Traversal', Lens, Lens', zoom)
1819
import Data.Maybe (Maybe(..))
1920
import Data.Symbol (SProxy(..))
2021
import Data.Tuple (Tuple(..), fst, snd)
@@ -39,6 +40,8 @@ bars = foo <<< _Just <<< bar <<< traversed
3940
type BarRec = { foo :: Array (Either { bar :: Array Int } String) }
4041
data Bar = Bar BarRec
4142

43+
newtype BarIndex = BarIndex Int
44+
4245
_Bar :: Lens' Bar BarRec
4346
_Bar = lens (\(Bar rec) -> rec) (\_ -> Bar)
4447

@@ -51,6 +54,10 @@ _0Justbar = _Bar <<< foo <<< ix 0
5154
_1bars :: Traversal' Bar Int
5255
_1bars = _0Justbar <<< _Left <<< bar <<< ix 1
5356

57+
_2Justbared :: IndexedTraversal' BarIndex Bar
58+
(Either { bar :: Array Int } String)
59+
_2Justbared = _Bar <<< foo <<< reindexed BarIndex itraversed
60+
5461
-- Tests state using zoom
5562
stateTest :: Tuple Int String
5663
stateTest = evalState go (Tuple 4 ["Foo", "Bar"]) where

0 commit comments

Comments
 (0)