module Data.List.ZipNonEmpty(
ZipNonEmpty,
ne,
zipNe,
usingNe,
usingZne
) where
import Data.List
import Data.Semigroup
import Data.Foldable
import Data.Typeable (Typeable)
import Data.Data (Data)
import Data.List.NonEmpty
import Control.Applicative
newtype ZipNonEmpty a = Z {
ne :: NonEmpty a
} deriving (Eq, Ord, Typeable, Data)
zipNe :: NonEmpty a ->
ZipNonEmpty a
zipNe = Z
usingNe :: (NonEmpty a ->
NonEmpty b) ->
ZipNonEmpty a ->
ZipNonEmpty b
usingNe = (zipNe .) . (. ne)
usingZne :: (ZipNonEmpty a ->
ZipNonEmpty b) ->
NonEmpty a ->
NonEmpty b
usingZne = (ne .) . (. zipNe)
instance (Show a) => Show (ZipNonEmpty a) where
show = show . ne
instance Semigroup (ZipNonEmpty a) where
Z a .++. Z b = Z (a .++. b)
instance Functor ZipNonEmpty where
fmap = usingNe . fmap
instance Applicative ZipNonEmpty where
pure = zipNe . unsafeToNonEmpty . repeat
f <*> a = let z = toList . ne
in zipNe . unsafeToNonEmpty $ zipWith id (z f) (z a)