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

Copyright(c) 2016-2019 Artyom Kazak
(c) 2019-2020 Kowainik
LicenseMPL-2.0
MaintainerKowainik <xrom.xkov@gmail.com>
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).

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 :: Applicative f => (Int -> a -> b -> f c) -> [a] -> [b] -> f [c] Source #

izipWithM_ :: Applicative f => (Int -> a -> b -> f c) -> [a] -> [b] -> f () Source #

Search

ifind :: (Int -> a -> Bool) -> [a] -> Maybe (Int, 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.

ireplicateM :: Applicative m => Int -> (Int -> m a) -> m [a] Source #

Perform a given action n times. Behaves like for_ [0..n-1], but avoids space leaks.

If you want more complicated loops (e.g. counting downwards), consider the loop package.

ireplicateM_ :: Monad m => Int -> (Int -> m a) -> m () Source #

NB. This function intentionally uses Monad even though Applicative is enough. That's because the transformers package didn't have an optimized definition of (*>) for StateT prior to 0.5.3.0, so for a common case of StateT this function would be 40 times slower with the Applicative constraint.

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 :: (Semigroup m, 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 #