eeue56/elm-lazy-list/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Lazy.List

Lazy list implementation in Elm.

Types

type alias LazyList a =
Lazy (LazyListView a)

Lazy List type.

= Nil
| Cons a (LazyList a)

Analogous to List type. This is the actual implementation type for the LazyList type. This type is exposed to the user if the user so wishes to do pattern matching or understand how the list type works. It is not recommended to work with this type directly. Try working solely with the provided functions in the package.

Constructors

cons : a -> LazyList a -> LazyList a

Add a value to the front of a list.

empty : LazyList a

Create an empty list.

singleton : a -> LazyList a

Create a singleton list.

Query operations

isEmpty : LazyList a -> Bool

Detect if a list is empty or not.

tail : LazyList a -> Maybe (LazyList a)

Get the tail of a list.

headAndTail : LazyList a -> Maybe ( a, LazyList a )

Get the head and tail of a list.

member : a -> LazyList a -> Bool

Test if a value is a member of a list.

length : LazyList a -> Int

Get the length of a lazy list.

Warning: This will not terminate if the list is infinite.

Conversions

toList : LazyList a -> List a

Convert a lazy list to a normal list.

fromList : List a -> LazyList a

Convert a normal list to a lazy list.

toArray : LazyList a -> Array a

Convert a lazy list to an array.

fromArray : Array a -> LazyList a

Convert an array to a lazy list.

Map-reduce et al.

map : (a -> b) -> LazyList a -> LazyList b

Map a function to a list.

zip : LazyList a -> LazyList b -> LazyList ( a, b )
reduce : (a -> b -> b) -> b -> LazyList a -> b

Reduce a list with a given reducer and an initial value.

Example : reduce (+) 0 (1 ::: 2 ::: 3 ::: 4 ::: empty) == 10

flatten : LazyList (LazyList a) -> LazyList a

Flatten a list of lists into a single list by appending all the inner lists into one big list.

append : LazyList a -> LazyList a -> LazyList a

Append a list to another list.

foldl : (a -> b -> b) -> b -> LazyList a -> b

Analogous to List.foldl. Is an alias for reduce.

foldr : (a -> b -> b) -> b -> LazyList a -> b

Analogous to List.foldr.

Common operations

intersperse : a -> LazyList a -> LazyList a

Places the given value between all members of the given list.

interleave : LazyList a -> LazyList a -> LazyList a

Interleave the elements of a list in another list. The two lists get interleaved at the end.

reverse : LazyList a -> LazyList a

Reverse a list.

cycle : LazyList a -> LazyList a

Take a list and repeat it ad infinitum. This cycles a finite list by putting the front after the end of the list. This results in a no-op in the case of an infinite list.

iterate : (a -> a) -> a -> LazyList a

Create an infinite list of applications of a function on some value.

Equivalent to:

x ::: f x ::: f (f x) ::: f (f (f x)) ::: ... -- etc...
repeat : a -> LazyList a

Repeat a value ad infinitum. Be careful when you use this. The result of this is a truly infinite list. Do not try calling reduce or toList on an infinite list as it'll never finish computing. Make sure you then filter it down to a finite list with head or take or something.

take : Int -> LazyList a -> LazyList a

Take at most n many values from a list.

takeWhile : (a -> Bool) -> LazyList a -> LazyList a

Take elements from a list as long as the predicate is satisfied.

drop : Int -> LazyList a -> LazyList a

Drop at most n many values from a list.

dropWhile : (a -> Bool) -> LazyList a -> LazyList a

Drop elements from a list as long as the predicate is satisfied.

Filtering operations

keepIf : (a -> Bool) -> LazyList a -> LazyList a

Keep all elements in a list that satisfy the given predicate.

dropIf : (a -> Bool) -> LazyList a -> LazyList a

Drop all elements in a list that satisfy the given predicate.

filterMap : (a -> Maybe b) -> LazyList a -> LazyList b

Map a function that may fail over a lazy list, keeping only the values that were successfully transformed.

unique : LazyList a -> LazyList a

Remove all duplicates from a list and return a list of distinct elements.

Chaining operations

andMap : LazyList a -> LazyList (a -> b) -> LazyList b

Known as mapN in some circles. Allows you to apply map in cases where then number of arguments are greater than 5.

The argument order is such that it works well with |> chains.

andThen : (a -> LazyList b) -> LazyList a -> LazyList b

Chain list producing operations. Map then flatten.

Useful math stuff

numbers : LazyList number

The infinite list of counting numbers.

i.e.:

1 ::: 2 ::: 3 ::: 4 ::: 5 ::: ... -- etc...
sum : LazyList number -> number

Get the sum of a list of numbers.

product : LazyList number -> number

Get the product of a list of numbers.

All the maps!

map2 : (a -> b -> c) -> LazyList a -> LazyList b -> LazyList c
map3 : (a -> b -> c -> d) -> LazyList a -> LazyList b -> LazyList c -> LazyList d
(a -> b -> c -> d -> e)
-> LazyList a
-> LazyList b
-> LazyList c
-> LazyList d
-> LazyList e
(a -> b -> c -> d -> e -> f)
-> LazyList a
-> LazyList b
-> LazyList c
-> LazyList d
-> LazyList e
-> LazyList f

All the zips!

zip3 : LazyList a -> LazyList b -> LazyList c -> LazyList ( a, b, c )
LazyList a
-> LazyList b
-> LazyList c
-> LazyList d
-> LazyList ( a, b, c, d )
LazyList a
-> LazyList b
-> LazyList c
-> LazyList d
-> LazyList e
-> LazyList ( a, b, c, d, e )

All the Cartesian products!

Warning: Calling these functions on large lists and then calling toList can easily overflow the stack. Consider passing the results to take aConstantNumber.

product2 : LazyList a -> LazyList b -> LazyList ( a, b )

Create a lazy list containing all possible pairs in the given lazy lists.

product3 : LazyList a -> LazyList b -> LazyList c -> LazyList ( a, b, c )

Create a lazy list containing all possible triples in the given lazy lists.

LazyList a
-> LazyList b
-> LazyList c
-> LazyList d
-> LazyList ( a, b, c, d )

Create a lazy list containing all possible 4-tuples in the given lazy lists.

LazyList a
-> LazyList b
-> LazyList c
-> LazyList d
-> LazyList e
-> LazyList ( a, b, c, d, e )

Create a lazy list containing all possible 5-tuples in the given lazy lists.

Infix Operators

(:::) : a -> LazyList a -> LazyList a

Alias for cons. Analogous to :: for lists.

(+++) : LazyList a -> LazyList a -> LazyList a

Alias for append. Analogous to ++ for lists.