| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Data.List.NonEmpty.Extra
Description
Extra functions for working with NonEmpty lists. The package
also exports the existing Data.List.NonEmpty functions.
Synopsis
- module Data.List.NonEmpty
- (|:) :: [a] -> a -> NonEmpty a
- (|>) :: NonEmpty a -> a -> NonEmpty a
- snoc :: NonEmpty a -> a -> NonEmpty a
- (!?) :: NonEmpty a -> Int -> Maybe a
- appendl :: NonEmpty a -> [a] -> NonEmpty a
- appendr :: [a] -> NonEmpty a -> NonEmpty a
- sortOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
- union :: Eq a => NonEmpty a -> NonEmpty a -> NonEmpty a
- unionBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a -> NonEmpty a
- nubOrd :: Ord a => NonEmpty a -> NonEmpty a
- nubOrdBy :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
- nubOrdOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
- maximum1 :: Ord a => NonEmpty a -> a
- minimum1 :: Ord a => NonEmpty a -> a
- maximumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
- minimumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
- maximumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a
- minimumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a
- foldl1' :: (a -> a -> a) -> NonEmpty a -> a
- repeatedly :: (NonEmpty a -> (b, [a])) -> NonEmpty a -> NonEmpty b
- compareLength :: NonEmpty a -> Int -> Ordering
Documentation
module Data.List.NonEmpty
(|:) :: [a] -> a -> NonEmpty a infixl 5 #
O(n). Append an element to a list.
[1,2,3] |: 4 |> 5 == 1 :| [2,3,4,5]
(|>) :: NonEmpty a -> a -> NonEmpty a infixl 5 #
O(n). Append an element to a non-empty list.
(1 :| [2,3]) |> 4 |> 5 == 1 :| [2,3,4,5]
(!?) :: NonEmpty a -> Int -> Maybe a infixl 9 #
A total variant of the list index function (!?).
(2 :| [3,4]) !? 1 == Just 3 (2 :| [3,4]) !? (-1) == Nothing (1 :| []) !? 1 == Nothing
appendl :: NonEmpty a -> [a] -> NonEmpty a #
Append a list to a non-empty list.
appendl (1 :| [2,3]) [4,5] == 1 :| [2,3,4,5]
appendr :: [a] -> NonEmpty a -> NonEmpty a #
Append a non-empty list to a list.
appendr [1,2,3] (4 :| [5]) == 1 :| [2,3,4,5]
sortOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a #
Sort a NonEmpty on a user-supplied projection of its elements.
See sortOn for more detailed information.
Examples
>>>sortOn fst $ (2, "world") :| [(4, "!"), (1, "Hello")](1,"Hello") :| [(2,"world"),(4,"!")]
>>>sortOn length $ "jim" :| ["creed", "pam", "michael", "dwight", "kevin"]"jim" :| ["pam","creed","kevin","dwight","michael"]
Performance notes
This function minimises the projections performed, by materialising the projections in an intermediate list.
For trivial projections, you should prefer using sortBy with
comparing, for example:
>>>sortBy (comparing fst) $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]
Or, for the exact same API as sortOn, you can use `sortBy . comparing`:
>>>(sortBy . comparing) fst $ (3, 1) :| [(2, 2), (1, 3)](1,3) :| [(2,2),(3,1)]
sortWith is an alias for `sortBy . comparing`.
Since: base-4.20.0.0
union :: Eq a => NonEmpty a -> NonEmpty a -> NonEmpty a #
Return the union of two non-empty lists. Duplicates, and elements of the first list, are removed from the the second list, but if the first list contains duplicates, so will the result.
(1 :| [3, 5, 3]) `union` (4 :| [5, 3, 5, 2]) == 1 :| [3, 5, 3, 4, 2]
unionBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a -> NonEmpty a #
The non-overloaded version of union.
maximumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a #
The largest element of a non-empty list with respect to the given comparison function.
minimumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a #
The least element of a non-empty list with respect to the given comparison function.
maximumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a #
A version of maximum1 where the comparison is done on some extracted value.
minimumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a #
A version of minimum1 where the comparison is done on some extracted value.
repeatedly :: (NonEmpty a -> (b, [a])) -> NonEmpty a -> NonEmpty b #
Apply some operation repeatedly, producing an element of output and the remainder of the list.
compareLength :: NonEmpty a -> Int -> Ordering #
Use compareLength xs n as a safer and faster alternative
to compare (length xs) n. Similarly, it's better
to write compareLength xs 10 == LT instead of length xs < 10.
While length would force and traverse
the entire spine of xs (which could even diverge if xs is infinite),
compareLength traverses at most n elements to determine its result.
>>>compareLength ('a' :| []) 1EQ>>>compareLength ('a' :| ['b']) 3LT>>>compareLength (0 :| [1..]) 100GT>>>compareLength undefined 0GT>>>compareLength ('a' :| 'b' : undefined) 1GT
Since: 4.21.0.0