Skip to content

Commit 7879b8d

Browse files
Merge pull request #106 from cscalfani/patch-1
Add Index instance for List
2 parents 83a242f + 81024d7 commit 7879b8d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/Data/Lens/Index.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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(..), (:))
1314
import Data.Map as M
1415
import Data.Maybe (Maybe, maybe, fromMaybe)
1516
import Data.Set as S
@@ -76,6 +77,14 @@ instance indexNonEmptyArray :: Index (NEA.NonEmptyArray a) Int a where
7677
(pure xs)
7778
(coalg >>> map \x -> fromMaybe xs (NEA.updateAt n x xs))
7879

80+
instance indexList :: Index (List a) Int a where
81+
ix n | n < 0 = wander \_ xs -> pure xs
82+
| otherwise = wander \coalg xs -> go xs n coalg where
83+
go :: forall f. Applicative f => List a -> Int -> (a -> f a) -> f (List a)
84+
go Nil _ _ = pure Nil
85+
go (a:as) 0 coalg = coalg a <#> (_:as)
86+
go (a:as) i coalg = (a:_) <$> (go as (i - 1) coalg)
87+
7988
instance indexSet :: Ord a => Index (S.Set a) a Unit where
8089
ix x =
8190
wander \coalg ->

0 commit comments

Comments
 (0)