import qualified RIO.List as LThis module does not export any partial functions. For those, see RIO.List.Partial
>>> listToMaybe [] Nothing
>>> listToMaybe [9] Just 9
>>> listToMaybe [1,2,3] Just 1Composing 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]
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)))]
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")]
fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")] fromListWith (++) [] == empty
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
toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
toDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
toList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")] toList empty == []
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
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
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
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
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
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
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
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