ilist-0.1.0.0: Optimised list functions for doing index-related things

Safe HaskellNone
LanguageHaskell2010

Data.List.Index

Contents

Description

Note: a lot of these functions are available for other types (in their respective packages):

Synopsis

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.

updateAt :: Int -> (a -> Maybe a) -> [a] -> [a] Source

updateAt applies a function to the element at the index, and then either replaces the element or deletes it (if the function has returned Nothing).

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.Listimap, 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

imap :: (Int -> a -> b) -> [a] -> [b] Source

Subject to fusion.

imapM :: Monad m => (Int -> a -> m b) -> [a] -> m [b] Source

imapM_ :: Monad m => (Int -> a -> m b) -> [a] -> m () Source

Subject to fusion.

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

ifoldr :: (Int -> a -> b -> b) -> b -> [a] -> b Source

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.

ifoldl' :: forall a b. (b -> Int -> a -> b) -> b -> [a] -> b Source

Subject to fusion.

iall :: (Int -> a -> Bool) -> [a] -> Bool Source

Subject to fusion.

iany :: (Int -> a -> Bool) -> [a] -> Bool Source

Subject to fusion.

iconcatMap :: (Int -> a -> [b]) -> [a] -> [b] Source

Sublists

ifilter :: (Int -> a -> Bool) -> [a] -> [a] Source

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 [c] Source

izipWithM_ :: Monad m => (Int -> a -> b -> m c) -> [a] -> [b] -> m () Source

Search

ifind :: (Int -> a -> Bool) -> [a] -> Maybe a Source

ifindIndex :: (Int -> a -> Bool) -> [a] -> Maybe Int Source

ifindIndices :: (Int -> a -> Bool) -> [a] -> [Int] Source

Less commonly used functions

Zipping

izipWith3 :: (Int -> a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d] Source

izipWith4 :: (Int -> a -> b -> c -> d -> e) -> [a] -> [b] -> [c] -> [d] -> [e] Source

izipWith5 :: (Int -> a -> b -> c -> d -> e -> f) -> [a] -> [b] -> [c] -> [d] -> [e] -> [f] Source

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

iforM :: Monad m => [a] -> (Int -> a -> m b) -> m [b] Source

iforM_ :: Monad m => [a] -> (Int -> a -> m b) -> m () Source

Subject to fusion.

itraverse :: Applicative m => (Int -> a -> m b) -> [a] -> m [b] Source

itraverse_ :: Applicative m => (Int -> a -> m b) -> [a] -> m () Source

Subject to fusion.

ifoldrM :: Monad m => (Int -> a -> b -> m b) -> b -> [a] -> m b Source

ifoldlM :: Monad m => (b -> Int -> a -> m b) -> b -> [a] -> m b Source

Subject to fusion.

Folds

ifoldMap :: Monoid m => (Int -> a -> m) -> [a] -> m Source

imapAccumR :: (acc -> Int -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) Source

imapAccumL :: (acc -> Int -> x -> (acc, y)) -> acc -> [x] -> (acc, [y]) Source