{-# LANGUAGE CPP #-}
module Data.List.NonEmpty.Extra(
module Data.List.NonEmpty,
(|:), (|>), snoc, (!?),
appendl, appendr,
sortOn, union, unionBy,
nubOrd, nubOrdBy, nubOrdOn,
maximum1, minimum1, maximumBy1, minimumBy1, maximumOn1, minimumOn1,
foldl1', repeatedly
) where
import Data.Function
import qualified Data.List.Extra as List
import Data.List.NonEmpty
#if __GLASGOW_HASKELL__ <= 802
import Data.Semigroup ((<>))
#endif
infixl 5 |>, |:
(|>) :: NonEmpty a -> a -> NonEmpty a
|> :: forall a. NonEmpty a -> a -> NonEmpty a
(|>) NonEmpty a
xs a
x = NonEmpty a
xs forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
snoc :: NonEmpty a -> a -> NonEmpty a
snoc :: forall a. NonEmpty a -> a -> NonEmpty a
snoc = forall a. NonEmpty a -> a -> NonEmpty a
(|>)
(|:) :: [a] -> a -> NonEmpty a
|: :: forall a. [a] -> a -> NonEmpty a
(|:) [a]
xs a
x = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. a -> NonEmpty a -> NonEmpty a
cons (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x) [a]
xs
(!?) :: NonEmpty a -> Int -> Maybe a
!? :: forall a. NonEmpty a -> Int -> Maybe a
(!?) ~(a
x :| [a]
xs) Int
n
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = forall a. a -> Maybe a
Just a
x
| Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = [a]
xs forall a. [a] -> Int -> Maybe a
List.!? (Int
n forall a. Num a => a -> a -> a
- Int
1)
| Bool
otherwise = forall a. Maybe a
Nothing
infixl 9 !?
appendl :: NonEmpty a -> [a] -> NonEmpty a
appendl :: forall a. NonEmpty a -> [a] -> NonEmpty a
appendl (a
x :| [a]
xs) [a]
l = a
x forall a. a -> [a] -> NonEmpty a
:| ([a]
xs forall a. [a] -> [a] -> [a]
++ [a]
l)
appendr :: [a] -> NonEmpty a -> NonEmpty a
appendr :: forall a. [a] -> NonEmpty a -> NonEmpty a
appendr [a]
l NonEmpty a
nel = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. a -> NonEmpty a -> NonEmpty a
cons NonEmpty a
nel [a]
l
sortOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
sortOn :: forall b a. Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
sortOn a -> b
f = forall a. [a] -> NonEmpty a
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Ord b => (a -> b) -> [a] -> [a]
List.sortOn a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NonEmpty a -> [a]
toList
union :: Eq a => NonEmpty a -> NonEmpty a -> NonEmpty a
union :: forall a. Eq a => NonEmpty a -> NonEmpty a -> NonEmpty a
union = forall a.
(a -> a -> Bool) -> NonEmpty a -> NonEmpty a -> NonEmpty a
unionBy forall a. Eq a => a -> a -> Bool
(==)
nubOrd :: Ord a => NonEmpty a -> NonEmpty a
nubOrd :: forall a. Ord a => NonEmpty a -> NonEmpty a
nubOrd = forall a. (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
nubOrdBy forall a. Ord a => a -> a -> Ordering
compare
nubOrdBy :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
nubOrdBy :: forall a. (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
nubOrdBy a -> a -> Ordering
cmp = forall a. [a] -> NonEmpty a
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
List.nubOrdBy a -> a -> Ordering
cmp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NonEmpty a -> [a]
toList
nubOrdOn :: Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
nubOrdOn :: forall b a. Ord b => (a -> b) -> NonEmpty a -> NonEmpty a
nubOrdOn a -> b
f = forall a. [a] -> NonEmpty a
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Ord b => (a -> b) -> [a] -> [a]
List.nubOrdOn a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NonEmpty a -> [a]
toList
unionBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a -> NonEmpty a
unionBy :: forall a.
(a -> a -> Bool) -> NonEmpty a -> NonEmpty a -> NonEmpty a
unionBy a -> a -> Bool
eq NonEmpty a
xs NonEmpty a
ys = forall a. [a] -> NonEmpty a
fromList forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> Bool) -> [a] -> [a] -> [a]
List.unionBy a -> a -> Bool
eq (forall a. NonEmpty a -> [a]
toList NonEmpty a
xs) (forall a. NonEmpty a -> [a]
toList NonEmpty a
ys)
maximum1 :: Ord a => NonEmpty a -> a
maximum1 :: forall a. Ord a => NonEmpty a -> a
maximum1 = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
List.maximum
minimum1 :: Ord a => NonEmpty a -> a
minimum1 :: forall a. Ord a => NonEmpty a -> a
minimum1 = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
List.minimum
maximumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
maximumBy1 :: forall a. (a -> a -> Ordering) -> NonEmpty a -> a
maximumBy1 = forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
List.maximumBy
minimumBy1 :: (a -> a -> Ordering) -> NonEmpty a -> a
minimumBy1 :: forall a. (a -> a -> Ordering) -> NonEmpty a -> a
minimumBy1 = forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
List.minimumBy
maximumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a
maximumOn1 :: forall b a. Ord b => (a -> b) -> NonEmpty a -> a
maximumOn1 a -> b
f = forall a. (a -> a -> Ordering) -> NonEmpty a -> a
maximumBy1 (forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
minimumOn1 :: Ord b => (a -> b) -> NonEmpty a -> a
minimumOn1 :: forall b a. Ord b => (a -> b) -> NonEmpty a -> a
minimumOn1 a -> b
f = forall a. (a -> a -> Ordering) -> NonEmpty a -> a
minimumBy1 (forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
foldl1' :: (a -> a -> a) -> NonEmpty a -> a
foldl1' :: forall a. (a -> a -> a) -> NonEmpty a -> a
foldl1' a -> a -> a
f (a
x:|[a]
xs) = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' a -> a -> a
f a
x [a]
xs
repeatedly :: (NonEmpty a -> (b, [a])) -> NonEmpty a -> NonEmpty b
repeatedly :: forall a b. (NonEmpty a -> (b, [a])) -> NonEmpty a -> NonEmpty b
repeatedly NonEmpty a -> (b, [a])
f (a
a :| [a]
as) = b
b forall a. a -> [a] -> NonEmpty a
:| forall a b. (NonEmpty a -> (b, [a])) -> [a] -> [b]
List.repeatedlyNE NonEmpty a -> (b, [a])
f [a]
as'
where (b
b, [a]
as') = NonEmpty a -> (b, [a])
f (a
a forall a. a -> [a] -> NonEmpty a
:| [a]
as)