Safe Haskell | None |
---|---|
Language | Haskell2010 |
Note: a lot of these functions are available for other types (in their respective packages):
Data.Vector
providesindexed
and lots of other functions beginning with “i”.Data.Map
andData.Sequence
provide similar functions, but use a different naming convention (e.g.mapWithKey
for maps andfoldrWithIndex
for sequences).- lens provides several typeclasses for indexed functions that work on maps, lists, vectors, bytestrings, and so on (in
Control.Lens.Indexed
), but unfortunately they are pretty slow for lists.
- indexed :: [a] -> [(Int, a)]
- deleteAt :: Int -> [a] -> [a]
- setAt :: Int -> a -> [a] -> [a]
- modifyAt :: Int -> (a -> a) -> [a] -> [a]
- updateAt :: Int -> (a -> Maybe a) -> [a] -> [a]
- insertAt :: Int -> a -> [a] -> [a]
- imap :: (Int -> a -> b) -> [a] -> [b]
- imapM :: Monad m => (Int -> a -> m b) -> [a] -> m [b]
- imapM_ :: Monad m => (Int -> a -> m b) -> [a] -> m ()
- ifor :: Applicative m => [a] -> (Int -> a -> m b) -> m [b]
- ifor_ :: Applicative m => [a] -> (Int -> a -> m b) -> m ()
- ifoldr :: (Int -> a -> b -> b) -> b -> [a] -> b
- ifoldl :: forall a b. (b -> Int -> a -> b) -> b -> [a] -> b
- ifoldl' :: forall a b. (b -> Int -> a -> b) -> b -> [a] -> b
- iall :: (Int -> a -> Bool) -> [a] -> Bool
- iany :: (Int -> a -> Bool) -> [a] -> Bool
- iconcatMap :: (Int -> a -> [b]) -> [a] -> [b]
- ifilter :: (Int -> a -> Bool) -> [a] -> [a]
- ipartition :: (Int -> a -> Bool) -> [a] -> ([a], [a])
- itakeWhile :: (Int -> a -> Bool) -> [a] -> [a]
- idropWhile :: (Int -> a -> Bool) -> [a] -> [a]
- izipWith :: (Int -> a -> b -> c) -> [a] -> [b] -> [c]
- izipWithM :: Monad m => (Int -> a -> b -> m c) -> [a] -> [b] -> m [c]
- izipWithM_ :: Monad m => (Int -> a -> b -> m c) -> [a] -> [b] -> m ()
- ifind :: (Int -> a -> Bool) -> [a] -> Maybe a
- ifindIndex :: (Int -> a -> Bool) -> [a] -> Maybe Int
- ifindIndices :: (Int -> a -> Bool) -> [a] -> [Int]
- izipWith3 :: (Int -> a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
- izipWith4 :: (Int -> a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e]
- izipWith5 :: (Int -> a -> b -> c -> d -> e -> f) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f]
- izipWith6 :: (Int -> a -> b -> c -> d -> e -> f -> g) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g]
- izipWith7 :: (Int -> a -> b -> c -> d -> e -> f -> g -> h) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [h]
- iforM :: Monad m => [a] -> (Int -> a -> m b) -> m [b]
- iforM_ :: Monad m => [a] -> (Int -> a -> m b) -> m ()
- itraverse :: Applicative m => (Int -> a -> m b) -> [a] -> m [b]
- itraverse_ :: Applicative m => (Int -> a -> m b) -> [a] -> m ()
- ifoldrM :: Monad m => (Int -> a -> b -> m b) -> b -> [a] -> m b
- ifoldlM :: Monad m => (b -> Int -> a -> m b) -> b -> [a] -> m b
- ifoldMap :: Monoid m => (Int -> a -> m) -> [a] -> m
- imapAccumR :: (acc -> Int -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
- imapAccumL :: (acc -> Int -> x -> (acc, y)) -> acc -> [x] -> (acc, [y])
Original functions
indexed :: [a] -> [(Int, a)] Source
indexed
pairs each element with its index.
>>>
indexed "hello"
[(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')]
Subject to fusion.
deleteAt :: Int -> [a] -> [a] Source
deleteAt
deletes the element at an index.
If the index is negative or exceeds list length, the original list will be returned.
setAt :: Int -> a -> [a] -> [a] Source
setAt
sets the element at the index.
If the index is negative or exceeds list length, the original list will be returned.
modifyAt :: Int -> (a -> a) -> [a] -> [a] Source
modifyAt
applies a function to the element at the index.
If the index is negative or exceeds list length, the original list will be returned.
insertAt :: Int -> a -> [a] -> [a] Source
insertAt
inserts an element at the given position:
(insertAt i x xs) !! i == x
If the index is negative or exceeds list length, the original list will be returned. (If the index is equal to the list length, the insertion can be carried out.)
Adapted functions from Data.List
These functions mimic their counterparts in Data.List – imap
, for instance, works like map
but gives the index of the element to the modifying function.
Note that left folds have the index argument after the accumulator argument – that's the convention adopted by containers and vector (but not lens).
Maps
ifor :: Applicative m => [a] -> (Int -> a -> m b) -> m [b] Source
ifor_ :: Applicative m => [a] -> (Int -> a -> m b) -> m () Source
Subject to fusion.
Folds
ifoldl :: forall a b. (b -> Int -> a -> b) -> b -> [a] -> b Source
The index isn't the first argument of the function because that's the convention adopted by containers and vector (but not lens).
Subject to fusion.
iconcatMap :: (Int -> a -> [b]) -> [a] -> [b] Source
Sublists
ipartition :: (Int -> a -> Bool) -> [a] -> ([a], [a]) Source
itakeWhile :: (Int -> a -> Bool) -> [a] -> [a] Source
idropWhile :: (Int -> a -> Bool) -> [a] -> [a] Source
Zipping
izipWith :: (Int -> a -> b -> c) -> [a] -> [b] -> [c] Source
Subject to fusion in the first argument.
izipWithM_ :: Monad m => (Int -> a -> b -> m c) -> [a] -> [b] -> m () Source
Search
ifindIndices :: (Int -> a -> Bool) -> [a] -> [Int] Source
Less commonly used functions
Zipping
izipWith6 :: (Int -> a -> b -> c -> d -> e -> f -> g) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] Source
izipWith7 :: (Int -> a -> b -> c -> d -> e -> f -> g -> h) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] -> [g] -> [h] Source
Monadic functions
itraverse :: Applicative m => (Int -> a -> m b) -> [a] -> m [b] Source
itraverse_ :: Applicative m => (Int -> a -> m b) -> [a] -> m () Source
Subject to fusion.
Folds
imapAccumR :: (acc -> Int -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) Source
imapAccumL :: (acc -> Int -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) Source