semialign-indexed 1 → 1.1
raw patch · 3 files changed
+50/−16 lines, 3 filesdep ~basedep ~lensdep ~semialign
Dependency ranges changed: base, lens, semialign, these
Files
- CHANGELOG.md +5/−0
- semialign-indexed.cabal +21/−7
- src/Data/Semialign/Indexed.hs +24/−9
CHANGELOG.md view
@@ -1,3 +1,8 @@+# 1.1++- Split `ZipWithIndex` out of `SemialignWithIndex`+- Compatible `semialign-1.1`.+ # 1 Split out of `these` package.
semialign-indexed.cabal view
@@ -1,7 +1,7 @@ cabal-version: >=1.10 name: semialign-indexed -version: 1-synopsis: SemialignWithIndex, i.e. izip and ialign+version: 1.1+synopsis: SemialignWithIndex, i.e. izipWith and ialignWith homepage: https://github.com/isomorphism/these license: BSD3 license-file: LICENSE@@ -16,13 +16,27 @@ @ class (FunctorWithIndex i f, Semialign f) => SemialignWithIndex i f | f -> i where \ ialignWith :: (i -> These a b -> c) -> f a -> f b -> f c+ @+ .+ @+ class (SemialignWithIndex i f, Zip f) => ZipWithIndex i f | f -> i where \ izipWith :: (i -> a -> b -> c) -> f a -> f b -> f c @ . Superclass @FunctorWithIndex@ is from @lens@ package.+ See https://hackage.haskell.org/package/semialign-optics for @optics@ variant. tested-with:- GHC ==7.4.2 || ==7.6.3 || ==7.8.4 || ==7.10.3 || ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.1+ GHC ==7.4.2+ || ==7.6.3+ || ==7.8.4+ || ==7.10.3+ || ==8.0.2+ || ==8.2.2+ || ==8.4.4+ || ==8.6.5+ || ==8.8.1+ , GHCJS ==8.4 source-repository head type: git@@ -40,17 +54,17 @@ -- ghc boot libs build-depends:- base >=4.5.1.0 && <4.13+ base >=4.5.1.0 && <4.14 , containers >=0.4.2.1 && <0.7 build-depends:- semialign >=1 && <1.1- , these >=1 && <1.1+ semialign >=1.1 && <1.2+ , these >=1 && <1.1 -- other dependencies build-depends: hashable >=1.2.7.0 && <1.4- , lens >=4.17 && <4.18+ , lens >=4.17 && <4.19 , unordered-containers >=0.2.8.0 && <0.3 , vector >=0.12.0.2 && <0.13
src/Data/Semialign/Indexed.hs view
@@ -5,14 +5,16 @@ -- | Zipping and aligning of indexed functors. module Data.Semialign.Indexed ( SemialignWithIndex (..),+ ZipWithIndex (..), ) where -import Prelude hiding (zip, zipWith)+import Prelude hiding (repeat, zip, zipWith) import Control.Lens (FunctorWithIndex (imap)) import Data.Align import Data.These+import Data.Zip -- Instances import Control.Applicative (ZipList)@@ -37,6 +39,8 @@ ialignWith :: (i -> These a b -> c) -> f a -> f b -> f c ialignWith f a b = imap f (align a b) +-- | Indexed version of 'Zip'+class (SemialignWithIndex i f, Zip f) => ZipWithIndex i f | f -> i where -- | Analogous to 'zipWith', but also provides an index. izipWith :: (i -> a -> b -> c) -> f a -> f b -> f c izipWith f a b = imap (uncurry . f) (zip a b)@@ -46,41 +50,50 @@ ------------------------------------------------------------------------------- instance SemialignWithIndex () Maybe+instance ZipWithIndex () Maybe instance SemialignWithIndex Int []+instance ZipWithIndex Int [] instance SemialignWithIndex Int ZipList+instance ZipWithIndex Int ZipList ------------------------------------------------------------------------------- -- transformers ------------------------------------------------------------------------------- instance SemialignWithIndex () Identity+instance ZipWithIndex () Identity instance (SemialignWithIndex i f, SemialignWithIndex j g) => SemialignWithIndex (Either i j) (Product f g) where- izipWith f (Pair fa ga) (Pair fb gb) = Pair fc gc where- fc = izipWith (f . Left) fa fb- gc = izipWith (f . Right) ga gb- ialignWith f (Pair fa ga) (Pair fb gb) = Pair fc gc where fc = ialignWith (f . Left) fa fb gc = ialignWith (f . Right) ga gb -instance (SemialignWithIndex i f, SemialignWithIndex j g) => SemialignWithIndex (i, j) (Compose f g) where- izipWith f (Compose fga) (Compose fgb) = Compose fgc where- fgc = izipWith (\i -> izipWith (\j -> f (i, j))) fga fgb+instance (ZipWithIndex i f, ZipWithIndex j g) => ZipWithIndex (Either i j) (Product f g) where+ izipWith f (Pair fa ga) (Pair fb gb) = Pair fc gc where+ fc = izipWith (f . Left) fa fb+ gc = izipWith (f . Right) ga gb +instance (SemialignWithIndex i f, SemialignWithIndex j g) => SemialignWithIndex (i, j) (Compose f g) where ialignWith f (Compose fga) (Compose fgb) = Compose $ ialignWith g fga fgb where g i (This ga) = imap (\j -> f (i, j) . This) ga g i (That gb) = imap (\j -> f (i, j) . That) gb g i (These ga gb) = ialignWith (\j -> f (i, j)) ga gb +instance (ZipWithIndex i f, ZipWithIndex j g) => ZipWithIndex (i, j) (Compose f g) where+ izipWith f (Compose fga) (Compose fgb) = Compose fgc where+ fgc = izipWith (\i -> izipWith (\j -> f (i, j))) fga fgb+ ------------------------------------------------------------------------------- -- containers ------------------------------------------------------------------------------- instance SemialignWithIndex Int Seq-instance SemialignWithIndex Int IntMap where+instance ZipWithIndex Int Seq+instance SemialignWithIndex Int IntMap+instance ZipWithIndex Int IntMap where izipWith = IntMap.intersectionWithKey instance Ord k => SemialignWithIndex k (Map k) where+instance Ord k => ZipWithIndex k (Map k) where izipWith = Map.intersectionWithKey -------------------------------------------------------------------------------@@ -88,6 +101,7 @@ ------------------------------------------------------------------------------- instance (Eq k, Hashable k) => SemialignWithIndex k (HashMap k) where+instance (Eq k, Hashable k) => ZipWithIndex k (HashMap k) where izipWith = HM.intersectionWithKey -------------------------------------------------------------------------------@@ -95,4 +109,5 @@ ------------------------------------------------------------------------------- instance SemialignWithIndex Int Vector where+instance ZipWithIndex Int Vector where izipWith = V.izipWith