Data.List.Lazy
- Package
- purescript-lists
- Repository
- purescript/purescript-lists
This module defines a type of lazy linked lists, and associated helper functions and type class instances.
Note: Depending on your use-case, you may prefer to use
Data.Sequence
instead, which might give better performance for certain
use cases. This module is an improvement over Data.Array
when working with
immutable lists of data in a purely-functional setting, but does not have
good random-access performance.
#toUnfoldable Source
toUnfoldable :: forall f. Unfoldable f => List ~> f
Convert a list into any unfoldable structure.
Running time: O(n)
#fromFoldable Source
fromFoldable :: forall f. Foldable f => f ~> List
Construct a list from a foldable structure.
Running time: O(n)
#(..) Source
Operator alias for Data.List.Lazy.range (non-associative / precedence 8)
An infix synonym for range
.
#replicateM Source
replicateM :: forall a m. Monad m => Int -> m a -> m (List a)
Perform a monadic action n
times collecting all of the results.
#some Source
some :: forall a f. Alternative f => Lazy (f (List a)) => f a -> f (List a)
Attempt a computation multiple times, requiring at least one success.
The Lazy
constraint is used to generate the result lazily, to ensure
termination.
#many Source
many :: forall a f. Alternative f => Lazy (f (List a)) => f a -> f (List a)
Attempt a computation multiple times, returning as many successful results as possible (possibly zero).
The Lazy
constraint is used to generate the result lazily, to ensure
termination.
#(!!) Source
Operator alias for Data.List.Lazy.index (left-associative / precedence 8)
An infix synonym for index
.
#elemLastIndex Source
elemLastIndex :: forall a. Eq a => a -> List a -> Maybe Int
Find the index of the last element equal to the specified element.
#findLastIndex Source
findLastIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int
Find the last index for which a predicate holds.
#insertAt Source
insertAt :: forall a. Int -> a -> List a -> List a
Insert an element into a list at the specified index, returning a new
list or Nothing
if the index is out-of-bounds.
This function differs from the strict equivalent in that out-of-bounds arguments result in the element being appended at the end of the list.
Running time: O(n)
#deleteAt Source
deleteAt :: forall a. Int -> List a -> List a
Delete an element from a list at the specified index, returning a new
list or Nothing
if the index is out-of-bounds.
This function differs from the strict equivalent in that out-of-bounds arguments result in the original list being returned unchanged.
Running time: O(n)
#updateAt Source
updateAt :: forall a. Int -> a -> List a -> List a
Update the element at the specified index, returning a new
list or Nothing
if the index is out-of-bounds.
This function differs from the strict equivalent in that out-of-bounds arguments result in the original list being returned unchanged.
Running time: O(n)
#modifyAt Source
modifyAt :: forall a. Int -> (a -> a) -> List a -> List a
Update the element at the specified index by applying a function to
the current value, returning a new list or Nothing
if the index is
out-of-bounds.
This function differs from the strict equivalent in that out-of-bounds arguments result in the original list being returned unchanged.
Running time: O(n)
#alterAt Source
alterAt :: forall a. Int -> (a -> Maybe a) -> List a -> List a
Update or delete the element at the specified index by applying a
function to the current value, returning a new list or Nothing
if the
index is out-of-bounds.
This function differs from the strict equivalent in that out-of-bounds arguments result in the original list being returned unchanged.
Running time: O(n)
#span Source
span :: forall a. (a -> Boolean) -> List a -> { init :: List a, rest :: List a }
Split a list into two parts:
- the longest initial segment for which all elements satisfy the specified predicate
- the remaining elements
For example,
span (\n -> n % 2 == 1) (1 : 3 : 2 : 4 : 5 : Nil) == Tuple (1 : 3 : Nil) (2 : 4 : 5 : Nil)
Running time: O(n)
#group Source
group :: forall a. Eq a => List a -> List (NonEmptyList a)
Group equal, consecutive elements of a list into lists.
For example,
group (1 : 1 : 2 : 2 : 1 : Nil) == (1 : 1 : Nil) : (2 : 2 : Nil) : (1 : Nil) : Nil
Running time: O(n)
#groupBy Source
groupBy :: forall a. (a -> a -> Boolean) -> List a -> List (NonEmptyList a)
Group equal, consecutive elements of a list into lists, using the specified equivalence relation to determine equality.
Running time: O(n)
#difference Source
difference :: forall a. Eq a => List a -> List a -> List a
Delete the first occurrence of each element in the second list from the first list.
Running time: O(n^2)
#intersectBy Source
intersectBy :: forall a. (a -> a -> Boolean) -> List a -> List a -> List a
Calculate the intersection of two lists, using the specified function to determine equality of elements.
Running time: O(n^2)
#zipWith Source
zipWith :: forall c b a. (a -> b -> c) -> List a -> List b -> List c
Apply a function to pairs of elements at the same positions in two lists, collecting the results in a new list.
If one list is longer, elements will be discarded from the longer list.
For example
zipWith (*) (1 : 2 : 3 : Nil) (4 : 5 : 6 : 7 Nil) == 4 : 10 : 18 : Nil
Running time: O(min(m, n))
#zipWithA Source
zipWithA :: forall c b a m. Applicative m => (a -> b -> m c) -> List a -> List b -> m (List c)
A generalization of zipWith
which accumulates results in some Applicative
functor.
#transpose Source
transpose :: forall a. List (List a) -> List (List a)
The 'transpose' function transposes the rows and columns of its argument. For example,
transpose ((1:2:3:nil) : (4:5:6:nil) : nil) ==
((1:4:nil) : (2:5:nil) : (3:6:nil) : nil)
If some of the rows are shorter than the following rows, their elements are skipped:
transpose ((10:11:nil) : (20:nil) : nil : (30:31:32:nil) : nil) ==
((10:20:30:nil) : (11:31:nil) : (32:nil) : nil)
Re-exports from Data.Foldable
#foldMap
foldMap :: forall f m a. Foldable f => Monoid m => (a -> m) -> f a -> m
#foldl
foldl :: forall f b a. Foldable f => (b -> a -> b) -> b -> f a -> b
#foldr
foldr :: forall f b a. Foldable f => (a -> b -> b) -> b -> f a -> b
#notElem
notElem :: forall f a. Foldable f => Eq a => a -> f a -> Boolean
Test whether a value is not an element of a data structure.
#intercalate
intercalate :: forall m f. Foldable f => Monoid m => m -> f m -> m
Fold a data structure, accumulating values in some Monoid
,
combining adjacent elements using the specified separator.
#fold
fold :: forall m f. Foldable f => Monoid m => f m -> m
Fold a data structure, accumulating values in some Monoid
.
#findMap
findMap :: forall f b a. Foldable f => (a -> Maybe b) -> f a -> Maybe b
Try to find an element in a data structure which satisfies a predicate mapping.
#find
find :: forall f a. Foldable f => (a -> Boolean) -> f a -> Maybe a
Try to find an element in a data structure which satisfies a predicate.
#elem
elem :: forall f a. Foldable f => Eq a => a -> f a -> Boolean
Test whether a value is an element of a data structure.
#any
any :: forall f b a. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b
any f
is the same as or <<< map f
; map a function over the structure,
and then get the disjunction of the results.
#all
all :: forall f b a. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b
all f
is the same as and <<< map f
; map a function over the structure,
and then get the conjunction of the results.
Re-exports from Data.List.Lazy.Types
#List Source
newtype List a
A lazy linked list.
Constructors
Instances
Newtype (List a) _
(Show a) => Show (List a)
(Eq a) => Eq (List a)
Eq1 List
(Ord a) => Ord (List a)
Ord1 List
Lazy (List a)
Semigroup (List a)
Monoid (List a)
Functor List
Foldable List
Unfoldable List
Traversable List
Apply List
Applicative List
Bind List
Monad List
Alt List
Plus List
Alternative List
MonadZero List
MonadPlus List
Extend List
#(:) Source
Operator alias for Data.List.Lazy.Types.cons (right-associative / precedence 6)
An infix alias for cons
; attaches an element to the front of
a list.
Running time: O(1)
Re-exports from Data.Traversable
#scanr
scanr :: forall f b a. Traversable f => (a -> b -> b) -> b -> f a -> f b
Fold a data structure from the right, keeping all intermediate results
instead of only the final result. Note that the initial value does not
appear in the result (unlike Haskell's Prelude.scanr
).
scanr (+) 0 [1,2,3] = [1,3,6]
scanr (flip (-)) 10 [1,2,3] = [4,5,7]
#scanl
scanl :: forall f b a. Traversable f => (b -> a -> b) -> b -> f a -> f b
Fold a data structure from the left, keeping all intermediate results
instead of only the final result. Note that the initial value does not
appear in the result (unlike Haskell's Prelude.scanl
).
scanl (+) 0 [1,2,3] = [1,3,6]
scanl (-) 10 [1,2,3] = [9,7,4]