List package:relude

Utility functions to work with lists and NonEmpty lists.
The listToMaybe function returns Nothing on an empty list or Just a where a is the first element of the list.

Examples

Basic usage:
>>> listToMaybe []
Nothing
>>> listToMaybe [9]
Just 9
>>> listToMaybe [1,2,3]
Just 1
Composing maybeToList with listToMaybe should be the identity on singleton/empty lists:
>>> maybeToList $ listToMaybe [5]
[5]

>>> maybeToList $ listToMaybe []
[]
But not on lists with more than one element:
>>> maybeToList $ listToMaybe [1,2,3]
[1]
Lists, but with an Applicative functor based on zipping.

Examples

In contrast to the Applicative for List:
>>> (+) <$> [1, 2, 3] <*> [4, 5, 6]
[5,6,7,6,7,8,7,8,9]
The Applicative instance of ZipList applies the operation by pairing up the elements, analogous to zipWithN
>>> (+) <$> ZipList [1, 2, 3] <*> ZipList [4, 5, 6]
ZipList {getZipList = [5,7,9]}
>>> (,,,) <$> ZipList [1, 2] <*> ZipList [3, 4] <*> ZipList [5, 6] <*> ZipList [7, 8]
ZipList {getZipList = [(1,3,5,7),(2,4,6,8)]}
>>> ZipList [(+1), (^2), (/ 2)] <*> ZipList [5, 5, 5]
ZipList {getZipList = [6.0,25.0,2.5]}
The IsList class and its methods are intended to be used in conjunction with the OverloadedLists extension. @since base-4.7.0.0
The fromList function constructs the structure l from the given list of Item l
The fromListN function takes the input list's length and potentially uses it to construct the structure l more efficiently compared to fromList. If the given number does not equal to the input list's length the behaviour of fromListN is not specified.
fromListN (length xs) xs == fromList xs
Collects the list of elements of a structure, from left to right.

Examples

Basic usage:
>>> biList (18, 42)
[18,42]
>>> biList (Left 18)
[18]
List of elements of a structure, from left to right. If the entire list is intended to be reduced via a fold, just fold the structure directly bypassing the list.

Examples

Basic usage:
>>> toList Nothing
[]
>>> toList (Just 42)
[42]
>>> toList (Left "foo")
[]
>>> toList (Node (Leaf 5) 17 (Node Empty 12 (Leaf 8)))
[5,17,12,8]
For lists, toList is the identity:
>>> toList [1, 2, 3]
[1,2,3]
@since base-4.8.0.0
The maybeToList function returns an empty list when given Nothing or a singleton list when given Just.

Examples

Basic usage:
>>> maybeToList (Just 7)
[7]
>>> maybeToList Nothing
[]
One can use maybeToList to avoid pattern matching when combined with a function that (safely) works on lists:
>>> import GHC.Internal.Text.Read ( readMaybe )

>>> sum $ maybeToList (readMaybe "3")
3

>>> sum $ maybeToList (readMaybe "")
0