Skip to content

Commit 9cb69f5

Browse files
authored
Add Index instance for List
Allow index lens on Lists the same way as Arrays.
1 parent afe51d4 commit 9cb69f5

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Data/Lens/Index.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import Data.Array.NonEmpty as NEA
1010
import Data.Identity (Identity)
1111
import Data.Lens.Internal.Wander (wander)
1212
import Data.Lens.Types (Traversal')
13+
import Data.List (List)
14+
import Data.List as L
1315
import Data.Map as M
1416
import Data.Maybe (Maybe, maybe, fromMaybe)
1517
import Data.Set as S
@@ -76,6 +78,14 @@ instance indexNonEmptyArray :: Index (NEA.NonEmptyArray a) Int a where
7678
(pure xs)
7779
(coalg >>> map \x -> fromMaybe xs (NEA.updateAt n x xs))
7880

81+
instance indexList :: Index (List a) Int a where
82+
ix n =
83+
wander \coalg xs ->
84+
xs L.!! n #
85+
maybe
86+
(pure xs)
87+
(coalg >>> map \x -> fromMaybe xs (L.updateAt n x xs))
88+
7989
instance indexSet :: Ord a => Index (S.Set a) a Unit where
8090
ix x =
8191
wander \coalg ->

0 commit comments

Comments
 (0)