{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
module Data.List.NonEmpty (
NonEmpty(..)
, map
, intersperse
, scanl
, scanr
, scanl1
, scanr1
, transpose
, sortBy
, sortWith
, length
, head
, tail
, last
, init
, singleton
, (<|), cons
, uncons
, unfoldr
, sort
, reverse
, inits
, tails
, append
, appendList
, prependList
, iterate
, repeat
, cycle
, unfold
, insert
, some1
, take
, drop
, splitAt
, takeWhile
, dropWhile
, span
, break
, filter
, partition
, group
, groupBy
, groupWith
, groupAllWith
, group1
, groupBy1
, groupWith1
, groupAllWith1
, isPrefixOf
, nub
, nubBy
, (!!)
, zip
, zipWith
, unzip
, fromList
, toList
, nonEmpty
, xor
) where
import Prelude hiding (break, cycle, drop, dropWhile,
filter, foldl, foldr, head, init, iterate,
last, length, map, repeat, reverse,
scanl, scanl1, scanr, scanr1, span,
splitAt, tail, take, takeWhile,
unzip, zip, zipWith, (!!))
import qualified Prelude
import Control.Applicative (Applicative (..), Alternative (many))
import Data.Foldable hiding (length, toList)
import qualified Data.Foldable as Foldable
import Data.Function (on)
import qualified Data.List as List
import Data.Ord (comparing)
import GHC.Base (NonEmpty(..))
import GHC.Stack.Types (HasCallStack)
infixr 5 <|
length :: NonEmpty a -> Int
length :: forall a. NonEmpty a -> Int
length (a
_ :| [a]
xs) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
xs
xor :: NonEmpty Bool -> Bool
xor :: NonEmpty Bool -> Bool
xor (Bool
x :| [Bool]
xs) = (Bool -> Bool -> Bool) -> Bool -> [Bool] -> Bool
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Bool -> Bool -> Bool
xor' Bool
x [Bool]
xs
where xor' :: Bool -> Bool -> Bool
xor' Bool
True Bool
y = Bool -> Bool
not Bool
y
xor' Bool
False Bool
y = Bool
y
unfold :: (a -> (b, Maybe a)) -> a -> NonEmpty b
unfold :: forall a b. (a -> (b, Maybe a)) -> a -> NonEmpty b
unfold a -> (b, Maybe a)
f a
a = case a -> (b, Maybe a)
f a
a of
(b
b, Maybe a
Nothing) -> b
b b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| []
(b
b, Just a
c) -> b
b b -> NonEmpty b -> NonEmpty b
forall a. a -> NonEmpty a -> NonEmpty a
<| (a -> (b, Maybe a)) -> a -> NonEmpty b
forall a b. (a -> (b, Maybe a)) -> a -> NonEmpty b
unfold a -> (b, Maybe a)
f a
c
{-# DEPRECATED unfold "Use unfoldr" #-}
nonEmpty :: [a] -> Maybe (NonEmpty a)
nonEmpty :: forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [] = Maybe (NonEmpty a)
forall a. Maybe a
Nothing
nonEmpty (a
a:[a]
as) = NonEmpty a -> Maybe (NonEmpty a)
forall a. a -> Maybe a
Just (a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
as)
uncons :: NonEmpty a -> (a, Maybe (NonEmpty a))
uncons :: forall a. NonEmpty a -> (a, Maybe (NonEmpty a))
uncons ~(a
a :| [a]
as) = (a
a, [a] -> Maybe (NonEmpty a)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty [a]
as)
unfoldr :: (a -> (b, Maybe a)) -> a -> NonEmpty b
unfoldr :: forall a b. (a -> (b, Maybe a)) -> a -> NonEmpty b
unfoldr a -> (b, Maybe a)
f a
a = case a -> (b, Maybe a)
f a
a of
(b
b, Maybe a
mc) -> b
b b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| [b] -> (a -> [b]) -> Maybe a -> [b]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] a -> [b]
go Maybe a
mc
where
go :: a -> [b]
go a
c = case a -> (b, Maybe a)
f a
c of
(b
d, Maybe a
me) -> b
d b -> [b] -> [b]
forall a. a -> [a] -> [a]
: [b] -> (a -> [b]) -> Maybe a -> [b]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] a -> [b]
go Maybe a
me
head :: NonEmpty a -> a
head :: forall a. NonEmpty a -> a
head (a
a :| [a]
_) = a
a
tail :: NonEmpty a -> [a]
tail :: forall a. NonEmpty a -> [a]
tail (a
_ :| [a]
as) = [a]
as
last :: NonEmpty a -> a
last :: forall a. NonEmpty a -> a
last ~(a
a :| [a]
as) = [a] -> a
forall a. HasCallStack => [a] -> a
List.last (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
as)
init :: NonEmpty a -> [a]
init :: forall a. NonEmpty a -> [a]
init ~(a
a :| [a]
as) = [a] -> [a]
forall a. HasCallStack => [a] -> [a]
List.init (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
as)
singleton :: a -> NonEmpty a
singleton :: forall a. a -> NonEmpty a
singleton a
a = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| []
(<|) :: a -> NonEmpty a -> NonEmpty a
a
a <| :: forall a. a -> NonEmpty a -> NonEmpty a
<| ~(a
b :| [a]
bs) = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| a
b a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
bs
cons :: a -> NonEmpty a -> NonEmpty a
cons :: forall a. a -> NonEmpty a -> NonEmpty a
cons = a -> NonEmpty a -> NonEmpty a
forall a. a -> NonEmpty a -> NonEmpty a
(<|)
sort :: Ord a => NonEmpty a -> NonEmpty a
sort :: forall a. Ord a => NonEmpty a -> NonEmpty a
sort = ([a] -> [a]) -> NonEmpty a -> NonEmpty a
forall (f :: * -> *) a b.
Foldable f =>
([a] -> [b]) -> f a -> NonEmpty b
lift [a] -> [a]
forall a. Ord a => [a] -> [a]
List.sort
fromList :: HasCallStack => [a] -> NonEmpty a
fromList :: forall a. HasCallStack => [a] -> NonEmpty a
fromList (a
a:[a]
as) = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
as
fromList [] = [Char] -> NonEmpty a
forall a. HasCallStack => [Char] -> a
error [Char]
"NonEmpty.fromList: empty list"
toList :: NonEmpty a -> [a]
toList :: forall a. NonEmpty a -> [a]
toList ~(a
a :| [a]
as) = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
as
lift :: Foldable f => ([a] -> [b]) -> f a -> NonEmpty b
lift :: forall (f :: * -> *) a b.
Foldable f =>
([a] -> [b]) -> f a -> NonEmpty b
lift [a] -> [b]
f = [b] -> NonEmpty b
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([b] -> NonEmpty b) -> (f a -> [b]) -> f a -> NonEmpty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [b]
f ([a] -> [b]) -> (f a -> [a]) -> f a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
map :: (a -> b) -> NonEmpty a -> NonEmpty b
map :: forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
map a -> b
f ~(a
a :| [a]
as) = a -> b
f a
a b -> [b] -> NonEmpty b
forall a. a -> [a] -> NonEmpty a
:| (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f [a]
as
inits :: Foldable f => f a -> NonEmpty [a]
inits :: forall (f :: * -> *) a. Foldable f => f a -> NonEmpty [a]
inits = [[a]] -> NonEmpty [a]
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([[a]] -> NonEmpty [a]) -> (f a -> [[a]]) -> f a -> NonEmpty [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [[a]]
forall a. [a] -> [[a]]
List.inits ([a] -> [[a]]) -> (f a -> [a]) -> f a -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
tails :: Foldable f => f a -> NonEmpty [a]
tails :: forall (f :: * -> *) a. Foldable f => f a -> NonEmpty [a]
tails = [[a]] -> NonEmpty [a]
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([[a]] -> NonEmpty [a]) -> (f a -> [[a]]) -> f a -> NonEmpty [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [[a]]
forall a. [a] -> [[a]]
List.tails ([a] -> [[a]]) -> (f a -> [a]) -> f a -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
insert :: (Foldable f, Ord a) => a -> f a -> NonEmpty a
insert :: forall (f :: * -> *) a.
(Foldable f, Ord a) =>
a -> f a -> NonEmpty a
insert a
a = [a] -> NonEmpty a
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([a] -> NonEmpty a) -> (f a -> [a]) -> f a -> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a] -> [a]
forall a. Ord a => a -> [a] -> [a]
List.insert a
a ([a] -> [a]) -> (f a -> [a]) -> f a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
some1 :: Alternative f => f a -> f (NonEmpty a)
some1 :: forall (f :: * -> *) a. Alternative f => f a -> f (NonEmpty a)
some1 f a
x = (a -> [a] -> NonEmpty a) -> f a -> f [a] -> f (NonEmpty a)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
(:|) f a
x (f a -> f [a]
forall a. f a -> f [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many f a
x)
scanl :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b
scanl :: forall (f :: * -> *) b a.
Foldable f =>
(b -> a -> b) -> b -> f a -> NonEmpty b
scanl b -> a -> b
f b
z = [b] -> NonEmpty b
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([b] -> NonEmpty b) -> (f a -> [b]) -> f a -> NonEmpty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> a -> b) -> b -> [a] -> [b]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
List.scanl b -> a -> b
f b
z ([a] -> [b]) -> (f a -> [a]) -> f a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
scanr :: Foldable f => (a -> b -> b) -> b -> f a -> NonEmpty b
scanr :: forall (f :: * -> *) a b.
Foldable f =>
(a -> b -> b) -> b -> f a -> NonEmpty b
scanr a -> b -> b
f b
z = [b] -> NonEmpty b
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([b] -> NonEmpty b) -> (f a -> [b]) -> f a -> NonEmpty b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> b -> [a] -> [b]
forall a b. (a -> b -> b) -> b -> [a] -> [b]
List.scanr a -> b -> b
f b
z ([a] -> [b]) -> (f a -> [a]) -> f a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
scanl1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
scanl1 :: forall a. (a -> a -> a) -> NonEmpty a -> NonEmpty a
scanl1 a -> a -> a
f ~(a
a :| [a]
as) = [a] -> NonEmpty a
forall a. HasCallStack => [a] -> NonEmpty a
fromList ((a -> a -> a) -> a -> [a] -> [a]
forall b a. (b -> a -> b) -> b -> [a] -> [b]
List.scanl a -> a -> a
f a
a [a]
as)
scanr1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a
scanr1 :: forall a. (a -> a -> a) -> NonEmpty a -> NonEmpty a
scanr1 a -> a -> a
f ~(a
a :| [a]
as) = [a] -> NonEmpty a
forall a. HasCallStack => [a] -> NonEmpty a
fromList ((a -> a -> a) -> [a] -> [a]
forall a. (a -> a -> a) -> [a] -> [a]
List.scanr1 a -> a -> a
f (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
as))
intersperse :: a -> NonEmpty a -> NonEmpty a
intersperse :: forall a. a -> NonEmpty a -> NonEmpty a
intersperse a
a ~(a
b :| [a]
bs) = a
b a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| case [a]
bs of
[] -> []
[a]
_ -> a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a] -> [a]
forall a. a -> [a] -> [a]
List.intersperse a
a [a]
bs
iterate :: (a -> a) -> a -> NonEmpty a
iterate :: forall a. (a -> a) -> a -> NonEmpty a
iterate a -> a
f a
a = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| (a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
List.iterate a -> a
f (a -> a
f a
a)
cycle :: NonEmpty a -> NonEmpty a
cycle :: forall a. NonEmpty a -> NonEmpty a
cycle = [a] -> NonEmpty a
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([a] -> NonEmpty a)
-> (NonEmpty a -> [a]) -> NonEmpty a -> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a]
forall a. HasCallStack => [a] -> [a]
List.cycle ([a] -> [a]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
reverse :: NonEmpty a -> NonEmpty a
reverse :: forall a. NonEmpty a -> NonEmpty a
reverse = ([a] -> [a]) -> NonEmpty a -> NonEmpty a
forall (f :: * -> *) a b.
Foldable f =>
([a] -> [b]) -> f a -> NonEmpty b
lift [a] -> [a]
forall a. [a] -> [a]
List.reverse
repeat :: a -> NonEmpty a
repeat :: forall a. a -> NonEmpty a
repeat a
a = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| a -> [a]
forall a. a -> [a]
List.repeat a
a
take :: Int -> NonEmpty a -> [a]
take :: forall a. Int -> NonEmpty a -> [a]
take Int
n = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
List.take Int
n ([a] -> [a]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
drop :: Int -> NonEmpty a -> [a]
drop :: forall a. Int -> NonEmpty a -> [a]
drop Int
n = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
List.drop Int
n ([a] -> [a]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
splitAt :: Int -> NonEmpty a -> ([a],[a])
splitAt :: forall a. Int -> NonEmpty a -> ([a], [a])
splitAt Int
n = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
List.splitAt Int
n ([a] -> ([a], [a]))
-> (NonEmpty a -> [a]) -> NonEmpty a -> ([a], [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
takeWhile :: (a -> Bool) -> NonEmpty a -> [a]
takeWhile :: forall a. (a -> Bool) -> NonEmpty a -> [a]
takeWhile a -> Bool
p = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
List.takeWhile a -> Bool
p ([a] -> [a]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
dropWhile :: (a -> Bool) -> NonEmpty a -> [a]
dropWhile :: forall a. (a -> Bool) -> NonEmpty a -> [a]
dropWhile a -> Bool
p = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
List.dropWhile a -> Bool
p ([a] -> [a]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
span :: (a -> Bool) -> NonEmpty a -> ([a], [a])
span :: forall a. (a -> Bool) -> NonEmpty a -> ([a], [a])
span a -> Bool
p = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.span a -> Bool
p ([a] -> ([a], [a]))
-> (NonEmpty a -> [a]) -> NonEmpty a -> ([a], [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
break :: (a -> Bool) -> NonEmpty a -> ([a], [a])
break :: forall a. (a -> Bool) -> NonEmpty a -> ([a], [a])
break a -> Bool
p = (a -> Bool) -> NonEmpty a -> ([a], [a])
forall a. (a -> Bool) -> NonEmpty a -> ([a], [a])
span (Bool -> Bool
not (Bool -> Bool) -> (a -> Bool) -> a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)
filter :: (a -> Bool) -> NonEmpty a -> [a]
filter :: forall a. (a -> Bool) -> NonEmpty a -> [a]
filter a -> Bool
p = (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
List.filter a -> Bool
p ([a] -> [a]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
partition :: (a -> Bool) -> NonEmpty a -> ([a], [a])
partition :: forall a. (a -> Bool) -> NonEmpty a -> ([a], [a])
partition a -> Bool
p = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.partition a -> Bool
p ([a] -> ([a], [a]))
-> (NonEmpty a -> [a]) -> NonEmpty a -> ([a], [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
group :: (Foldable f, Eq a) => f a -> [NonEmpty a]
group :: forall (f :: * -> *) a. (Foldable f, Eq a) => f a -> [NonEmpty a]
group = (a -> a -> Bool) -> f a -> [NonEmpty a]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
groupBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
groupBy :: Foldable f => (a -> a -> Bool) -> f a -> [NonEmpty a]
groupBy :: forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
groupBy a -> a -> Bool
eq0 = (a -> a -> Bool) -> [a] -> [NonEmpty a]
forall {a}. (a -> a -> Bool) -> [a] -> [NonEmpty a]
go a -> a -> Bool
eq0 ([a] -> [NonEmpty a]) -> (f a -> [a]) -> f a -> [NonEmpty a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
where
go :: (a -> a -> Bool) -> [a] -> [NonEmpty a]
go a -> a -> Bool
_ [] = []
go a -> a -> Bool
eq (a
x : [a]
xs) = (a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
ys) NonEmpty a -> [NonEmpty a] -> [NonEmpty a]
forall a. a -> [a] -> [a]
: (a -> a -> Bool) -> [a] -> [NonEmpty a]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
groupBy a -> a -> Bool
eq [a]
zs
where ([a]
ys, [a]
zs) = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.span (a -> a -> Bool
eq a
x) [a]
xs
groupWith :: (Foldable f, Eq b) => (a -> b) -> f a -> [NonEmpty a]
groupWith :: forall (f :: * -> *) b a.
(Foldable f, Eq b) =>
(a -> b) -> f a -> [NonEmpty a]
groupWith a -> b
f = (a -> a -> Bool) -> f a -> [NonEmpty a]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
groupBy (b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==) (b -> b -> Bool) -> (a -> b) -> a -> a -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
groupAllWith :: (Ord b) => (a -> b) -> [a] -> [NonEmpty a]
groupAllWith :: forall b a. Ord b => (a -> b) -> [a] -> [NonEmpty a]
groupAllWith a -> b
f = (a -> b) -> [a] -> [NonEmpty a]
forall (f :: * -> *) b a.
(Foldable f, Eq b) =>
(a -> b) -> f a -> [NonEmpty a]
groupWith a -> b
f ([a] -> [NonEmpty a]) -> ([a] -> [a]) -> [a] -> [NonEmpty a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
List.sortBy (b -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (b -> b -> Ordering) -> (a -> b) -> a -> a -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
group1 :: Eq a => NonEmpty a -> NonEmpty (NonEmpty a)
group1 :: forall a. Eq a => NonEmpty a -> NonEmpty (NonEmpty a)
group1 = (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
forall a. (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupBy1 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
groupBy1 :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupBy1 :: forall a. (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupBy1 a -> a -> Bool
eq (a
x :| [a]
xs) = (a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
ys) NonEmpty a -> [NonEmpty a] -> NonEmpty (NonEmpty a)
forall a. a -> [a] -> NonEmpty a
:| (a -> a -> Bool) -> [a] -> [NonEmpty a]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
groupBy a -> a -> Bool
eq [a]
zs
where ([a]
ys, [a]
zs) = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.span (a -> a -> Bool
eq a
x) [a]
xs
groupWith1 :: (Eq b) => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupWith1 :: forall b a. Eq b => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupWith1 a -> b
f = (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
forall a. (a -> a -> Bool) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupBy1 (b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==) (b -> b -> Bool) -> (a -> b) -> a -> a -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
groupAllWith1 :: (Ord b) => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupAllWith1 :: forall b a.
Ord b =>
(a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupAllWith1 a -> b
f = (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
forall b a. Eq b => (a -> b) -> NonEmpty a -> NonEmpty (NonEmpty a)
groupWith1 a -> b
f (NonEmpty a -> NonEmpty (NonEmpty a))
-> (NonEmpty a -> NonEmpty a)
-> NonEmpty a
-> NonEmpty (NonEmpty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> NonEmpty a -> NonEmpty a
forall o a. Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
sortWith a -> b
f
isPrefixOf :: Eq a => [a] -> NonEmpty a -> Bool
isPrefixOf :: forall a. Eq a => [a] -> NonEmpty a -> Bool
isPrefixOf [] NonEmpty a
_ = Bool
True
isPrefixOf (a
y:[a]
ys) (a
x :| [a]
xs) = (a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x) Bool -> Bool -> Bool
&& [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
List.isPrefixOf [a]
ys [a]
xs
(!!) :: HasCallStack => NonEmpty a -> Int -> a
!! :: forall a. HasCallStack => NonEmpty a -> Int -> a
(!!) ~(a
x :| [a]
xs) Int
n
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = a
x
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = [a]
xs [a] -> Int -> a
forall a. HasCallStack => [a] -> Int -> a
List.!! (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
| Bool
otherwise = [Char] -> a
forall a. HasCallStack => [Char] -> a
error [Char]
"NonEmpty.!! negative index"
infixl 9 !!
zip :: NonEmpty a -> NonEmpty b -> NonEmpty (a,b)
zip :: forall a b. NonEmpty a -> NonEmpty b -> NonEmpty (a, b)
zip ~(a
x :| [a]
xs) ~(b
y :| [b]
ys) = (a
x, b
y) (a, b) -> [(a, b)] -> NonEmpty (a, b)
forall a. a -> [a] -> NonEmpty a
:| [a] -> [b] -> [(a, b)]
forall a b. [a] -> [b] -> [(a, b)]
List.zip [a]
xs [b]
ys
zipWith :: (a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
zipWith :: forall a b c.
(a -> b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c
zipWith a -> b -> c
f ~(a
x :| [a]
xs) ~(b
y :| [b]
ys) = a -> b -> c
f a
x b
y c -> [c] -> NonEmpty c
forall a. a -> [a] -> NonEmpty a
:| (a -> b -> c) -> [a] -> [b] -> [c]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
List.zipWith a -> b -> c
f [a]
xs [b]
ys
unzip :: Functor f => f (a,b) -> (f a, f b)
unzip :: forall (f :: * -> *) a b. Functor f => f (a, b) -> (f a, f b)
unzip f (a, b)
xs = ((a, b) -> a
forall a b. (a, b) -> a
fst ((a, b) -> a) -> f (a, b) -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (a, b)
xs, (a, b) -> b
forall a b. (a, b) -> b
snd ((a, b) -> b) -> f (a, b) -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (a, b)
xs)
nub :: Eq a => NonEmpty a -> NonEmpty a
nub :: forall a. Eq a => NonEmpty a -> NonEmpty a
nub = (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
forall a. (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
nubBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
nubBy :: (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
nubBy :: forall a. (a -> a -> Bool) -> NonEmpty a -> NonEmpty a
nubBy a -> a -> Bool
eq (a
a :| [a]
as) = a
a a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| (a -> a -> Bool) -> [a] -> [a]
forall a. (a -> a -> Bool) -> [a] -> [a]
List.nubBy a -> a -> Bool
eq ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
List.filter (\a
b -> Bool -> Bool
not (a -> a -> Bool
eq a
a a
b)) [a]
as)
transpose :: NonEmpty (NonEmpty a) -> NonEmpty (NonEmpty a)
transpose :: forall a. NonEmpty (NonEmpty a) -> NonEmpty (NonEmpty a)
transpose = ([a] -> NonEmpty a) -> NonEmpty [a] -> NonEmpty (NonEmpty a)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> NonEmpty a
forall a. HasCallStack => [a] -> NonEmpty a
fromList
(NonEmpty [a] -> NonEmpty (NonEmpty a))
-> (NonEmpty (NonEmpty a) -> NonEmpty [a])
-> NonEmpty (NonEmpty a)
-> NonEmpty (NonEmpty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[a]] -> NonEmpty [a]
forall a. HasCallStack => [a] -> NonEmpty a
fromList ([[a]] -> NonEmpty [a])
-> (NonEmpty (NonEmpty a) -> [[a]])
-> NonEmpty (NonEmpty a)
-> NonEmpty [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[a]] -> [[a]]
forall a. [[a]] -> [[a]]
List.transpose ([[a]] -> [[a]])
-> (NonEmpty (NonEmpty a) -> [[a]])
-> NonEmpty (NonEmpty a)
-> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty [a] -> [[a]]
forall a. NonEmpty a -> [a]
toList
(NonEmpty [a] -> [[a]])
-> (NonEmpty (NonEmpty a) -> NonEmpty [a])
-> NonEmpty (NonEmpty a)
-> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NonEmpty a -> [a]) -> NonEmpty (NonEmpty a) -> NonEmpty [a]
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList
sortBy :: (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
sortBy :: forall a. (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
sortBy a -> a -> Ordering
f = ([a] -> [a]) -> NonEmpty a -> NonEmpty a
forall (f :: * -> *) a b.
Foldable f =>
([a] -> [b]) -> f a -> NonEmpty b
lift ((a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
List.sortBy a -> a -> Ordering
f)
sortWith :: Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
sortWith :: forall o a. Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
sortWith = (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
forall a. (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
sortBy ((a -> a -> Ordering) -> NonEmpty a -> NonEmpty a)
-> ((a -> o) -> a -> a -> Ordering)
-> (a -> o)
-> NonEmpty a
-> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> o) -> a -> a -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing
append :: NonEmpty a -> NonEmpty a -> NonEmpty a
append :: forall a. NonEmpty a -> NonEmpty a -> NonEmpty a
append = NonEmpty a -> NonEmpty a -> NonEmpty a
forall a. Semigroup a => a -> a -> a
(<>)
appendList :: NonEmpty a -> [a] -> NonEmpty a
appendList :: forall a. NonEmpty a -> [a] -> NonEmpty a
appendList (a
x :| [a]
xs) [a]
ys = a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
xs [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a]
ys
prependList :: [a] -> NonEmpty a -> NonEmpty a
prependList :: forall a. [a] -> NonEmpty a -> NonEmpty a
prependList [a]
ls NonEmpty a
ne = case [a]
ls of
[] -> NonEmpty a
ne
(a
x : [a]
xs) -> a
x a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
xs [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
toList NonEmpty a
ne