List package:rio

List. Import as:
import qualified RIO.List as L
This module does not export any partial functions. For those, see RIO.List.Partial
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]
listen m is an action that executes the action m and adds its output to the value of the computation.
listens f m is an action that executes the action m and adds the result of applying f to the output to the value of the computation.
Convert a Deque into a list. Does not modify the Deque.
Construct a map with the supplied mappings. If the list contains duplicate mappings, the later mappings take precedence.
Construct a map from a list of elements. Uses the provided function f to merge duplicate entries with (f newVal oldVal).

Examples

Given a list xs, create a map with the number of occurrences of each element in xs:
let xs = ['a', 'b', 'a']
in fromListWith (+) [ (x, 1) | x <- xs ]

= fromList [('a', 2), ('b', 1)]
Given a list of key-value pairs xs :: [(k, v)], group all values by their keys and return a HashMap k [v].
let xs = ('a', 1), ('b', 2), ('a', 3)]
in fromListWith (++) [ (k, [v]) | (k, v) <- xs ]

= fromList [('a', [3, 1]), ('b', [2])]
Note that the lists in the resulting map contain elements in reverse order from their occurences in the original list. More generally, duplicate entries are accumulated as follows; this matters when f is not commutative or not associative.
fromListWith f [(k, a), (k, b), (k, c), (k, d)]
= fromList [(k, f d (f c (f b a)))]
Return a list of this map's elements. The list is produced lazily. The order of its elements is unspecified.
Construct a set from a list of elements.
Return a list of this set's elements. The list is produced lazily.
toListOf is a synonym for (^..).
O(n*log n). Build a map from a list of key/value pairs. See also fromAscList. If the list contains more than one value for the same key, the last value for the key is retained. If the keys of the list are ordered, linear-time implementation is used, with the performance equal to fromDistinctAscList.
fromList [] == empty
fromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
fromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
O(n*log n). Build a map from a list of key/value pairs with a combining function. See also fromAscListWith.
fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
fromListWith (++) [] == empty
O(n*log n). Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey.
let f k a1 a2 = (show k) ++ a1 ++ a2
fromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")]
fromListWithKey f [] == empty
O(n). Convert the map to a list of key/value pairs where the keys are in ascending order. Subject to list fusion.
toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
O(n). Convert the map to a list of key/value pairs where the keys are in descending order. Subject to list fusion.
toDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
O(n). Convert the map to a list of key/value pairs. Subject to list fusion.
toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
toList empty == []
O(n). Build a map from an ascending list in linear time. The precondition (input list is ascending) is not checked.
fromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
fromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
valid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True
valid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False
O(n). Build a map from an ascending list in linear time with a combining function for equal keys. The precondition (input list is ascending) is not checked.
fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
valid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True
valid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
O(n). Build a map from an ascending list in linear time with a combining function for equal keys. The precondition (input list is ascending) is not checked.
let f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
valid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True
valid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
O(n). Build a map from a descending list in linear time. The precondition (input list is descending) is not checked.
fromDescList [(5,"a"), (3,"b")]          == fromList [(3, "b"), (5, "a")]
fromDescList [(5,"a"), (5,"b"), (3,"a")] == fromList [(3, "b"), (5, "b")]
valid (fromDescList [(5,"a"), (5,"b"), (3,"b")]) == True
valid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False
O(n). Build a map from a descending list in linear time with a combining function for equal keys. The precondition (input list is descending) is not checked.
fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "ba")]
valid (fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")]) == True
valid (fromDescListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
O(n). Build a map from a descending list in linear time with a combining function for equal keys. The precondition (input list is descending) is not checked.
let f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
valid (fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")]) == True
valid (fromDescListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
O(n). Build a map from an ascending list of distinct elements in linear time. The precondition is not checked.
fromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
valid (fromDistinctAscList [(3,"b"), (5,"a")])          == True
valid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False
O(n). Build a map from a descending list of distinct elements in linear time. The precondition is not checked.
fromDistinctDescList [(5,"a"), (3,"b")] == fromList [(3, "b"), (5, "a")]
valid (fromDistinctDescList [(5,"a"), (3,"b")])          == True
valid (fromDistinctDescList [(5,"a"), (3,"b"), (3,"a")]) == False