Skip to content

Commit 7a9277d

Browse files
committed
Add unzip helpers
1 parent 12d1472 commit 7a9277d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

semialign/semialign.cabal

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ library
7979
, tagged >=0.8.6 && <0.9
8080
, unordered-containers >=0.2.8.0 && <0.3
8181
, vector >=0.12.0.2 && <0.13
82-
83-
-- base shims
84-
if !impl(ghc >=8.2)
85-
build-depends: bifunctors >=5.5.4 && <5.6
82+
, bifunctors >=5.5.4 && <5.6
8683

8784
if !impl(ghc >=8.0)
8885
build-depends:

semialign/src/Data/Semialign/Internal.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE DeriveFunctor #-}
23
{-# LANGUAGE FlexibleInstances #-}
34
{-# LANGUAGE FunctionalDependencies #-}
45
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
@@ -19,6 +20,7 @@ import qualified Prelude as Prelude
1920

2021
import Control.Applicative (ZipList (..), pure, (<$>))
2122
import Data.Bifunctor (Bifunctor (..))
23+
import Data.Biapplicative (Biapplicative (..), traverseBia)
2224
import Data.Functor.Compose (Compose (..))
2325
import Data.Functor.Identity (Identity (..))
2426
import Data.Functor.Product (Product (..))
@@ -577,6 +579,17 @@ instance (Ord k) => Align (Map k) where
577579
instance Ord k => Unalign (Map k) where
578580
unalign xs = (Map.mapMaybe justHere xs, Map.mapMaybe justThere xs)
579581

582+
-- A copy of (,) with a stricter bimap.
583+
newtype SBPair a b = SBPair { unSBPair :: (a, b) }
584+
585+
instance Bifunctor SBPair where
586+
bimap f g (SBPair (a, b)) = SBPair (f a, g b)
587+
588+
instance Biapplicative SBPair where
589+
bipure a b = SBPair (a, b)
590+
biliftA2 f g (SBPair (a, b)) (SBPair (c, d)) =
591+
SBPair (f a c, g b d)
592+
580593
instance Ord k => Unzip (Map k) where unzip = unzipDefault
581594

582595
instance Ord k => Zip (Map k) where

0 commit comments

Comments
 (0)