data-diverse-lens 3.1.1.0 → 4.0.0.0
raw patch · 5 files changed
+98/−95 lines, 5 filesdep ~data-diversedep ~generic-lensdep ~taggedPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: data-diverse, generic-lens, tagged
API changes (from Hackage documentation)
- Data.Diverse.Lens.Many: item :: Had a s => Lens s (Replaced a b s) a b
- Data.Diverse.Lens.Many: item' :: Has a s => Lens' s a
- Data.Diverse.Lens.Many: itemL :: HadL l a s => Lens s (ReplacedL l a b s) a b
- Data.Diverse.Lens.Many: itemL' :: HasL l a s => Lens' s a
- Data.Diverse.Lens.Many: itemN :: (HadN n a s, (HasPosition n s (ReplacedN n a b s) a b)) => Lens s (ReplacedN n a b s) a b
- Data.Diverse.Lens.Many: itemN' :: HasN n a s => Lens' s a
- Data.Diverse.Lens.Many: itemTag :: forall l a b s. Had (Tagged l a) s => Lens s (Replaced (Tagged l a) (Tagged l b) s) a b
- Data.Diverse.Lens.Many: itemTag' :: forall l a s. Has (Tagged l a) s => Lens' s a
- Data.Diverse.Profunctor.Many: itemized :: forall w a b s t. (Profunctor w, Strong w, Itemized a b s t) => w a b -> w s t
- Data.Diverse.Profunctor.Many: itemizedK :: forall m a b s t. (Monad m, Itemized a b s t) => (a -> m b) -> (s -> m t)
- Data.Diverse.Profunctor.Many: type Itemized a b s t = (Had a s, t ~ Replaced a b s)
+ Data.Diverse.Lens.Many: piece :: Had a s => Lens s (Replaced a b s) a b
+ Data.Diverse.Lens.Many: piece' :: Has a s => Lens' s a
+ Data.Diverse.Lens.Many: pieceL :: HadL l a s => Lens s (ReplacedL l a b s) a b
+ Data.Diverse.Lens.Many: pieceL' :: HasL l a s => Lens' s a
+ Data.Diverse.Lens.Many: pieceN :: (HadN n a s, (HasPosition n s (ReplacedN n a b s) a b)) => Lens s (ReplacedN n a b s) a b
+ Data.Diverse.Lens.Many: pieceN' :: HasN n a s => Lens' s a
+ Data.Diverse.Lens.Many: pieceTag :: forall l a b s. Had (Tagged l a) s => Lens s (Replaced (Tagged l a) (Tagged l b) s) a b
+ Data.Diverse.Lens.Many: pieceTag' :: forall l a s. Has (Tagged l a) s => Lens' s a
+ Data.Diverse.Profunctor.Many: pieced :: forall w a b s t. (Profunctor w, Strong w, Pieced a b s t) => w a b -> w s t
+ Data.Diverse.Profunctor.Many: piecedK :: forall m a b s t. (Monad m, Pieced a b s t) => (a -> m b) -> (s -> m t)
+ Data.Diverse.Profunctor.Many: type Pieced a b s t = (Had a s, t ~ Replaced a b s)
Files
- README.md +3/−0
- data-diverse-lens.cabal +7/−7
- src/Data/Diverse/Lens/Many.hs +38/−38
- src/Data/Diverse/Profunctor/Many.hs +12/−12
- test/Data/Diverse/Lens/ManySpec.hs +38/−38
README.md view
@@ -7,6 +7,9 @@ # Changelog +* 4.0.0.0+ - Renamed `itemXXX` to `pieceXXX`, `Itemized` to `Pieced`, `itemizedXXX` to `piecedXXX`.+ * 3.1.1.0 - `itemTag` only requires `Has` and likewise `facetTag` only requires `AsFacet`.
data-diverse-lens.cabal view
@@ -1,5 +1,5 @@ name: data-diverse-lens-version: 3.1.1.0+version: 4.0.0.0 synopsis: Isos & Lens for Data.Diverse.Many and Prisms for Data.Diverse.Which description: Isos & Lens for Data.Diverse.Many and Prisms for Data.Diverse.Which Refer to [ManySpec.hs](https://github.com/louispan/data-diverse-lens/blob/master/test/Data/Diverse/Lens/ManySpec.hs) and [WhichSpec.hs](https://github.com/louispan/data-diverse-lens/blob/master/test/Data/Diverse/Lens/WhichSpec.hs) for example usages.@@ -24,10 +24,10 @@ Data.Diverse.Profunctor.Many Data.Diverse.Profunctor.Which build-depends: base >= 4.7 && < 5- , data-diverse >= 3.1.0.0- , tagged >= 0.8.5+ , data-diverse >= 4.0+ , tagged >= 0.8 , profunctors >= 5.2- , generic-lens >= 0.5.0.0+ , generic-lens >= 0.5 , lens >= 4 , data-has >= 0.3 ghc-options: -Wall@@ -40,12 +40,12 @@ other-modules: Data.Diverse.Lens.ManySpec Data.Diverse.Lens.WhichSpec build-depends: base- , data-diverse >= 3.1.0.0+ , data-diverse >= 4.0 , data-diverse-lens , hspec >= 2 , lens >= 4- , tagged >= 0.8.5- , generic-lens >= 0.5.0.0+ , tagged >= 0.8+ , generic-lens >= 0.5 ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall default-language: Haskell2010
src/Data/Diverse/Lens/Many.hs view
@@ -24,9 +24,9 @@ -- * Single field -- ** Lens for a single field , Has(..)- , item'- , itemTag- , itemTag'+ , piece'+ , pieceTag+ , pieceTag' , Had(..) , HasL(..) , HadL(..)@@ -73,93 +73,93 @@ -- -- @ -- let x = (5 :: Int) './' False './' \'X' './' Just \'O' './' 'nil'--- x '^.' 'item'' \@Int \`shouldBe` 5--- (x '&' 'item'' \@Int .~ 6) \`shouldBe` (6 :: Int) './' False './' \'X' './' Just \'O' './' 'nil'+-- x '^.' 'piece'' \@Int \`shouldBe` 5+-- (x '&' 'piece'' \@Int .~ 6) \`shouldBe` (6 :: Int) './' False './' \'X' './' Just \'O' './' 'nil' -- @-item' :: Has a s => Lens' s a-item' = hasLens+piece' :: Has a s => Lens' s a+piece' = hasLens instance UniqueMember x xs => Has x (Many xs) where hasLens = lens grab replace' --- | Polymorphic version of 'item''+-- | Polymorphic version of 'piece'' class (Has a s, Replaced a a s ~ s) => Had a s where type Replaced a b s- item :: Lens s (Replaced a b s) a b+ piece :: Lens s (Replaced a b s) a b instance (UniqueMember x xs) => Had x (Many xs) where type Replaced x b (Many xs) = Many (Replace x b xs)- item = lens grab (replace @x)+ piece = lens grab (replace @x) -itemTag' :: forall l a s. Has (Tagged l a) s => Lens' s a-itemTag' = item' @(Tagged l a) . iso unTagged Tagged+pieceTag' :: forall l a s. Has (Tagged l a) s => Lens' s a+pieceTag' = piece' @(Tagged l a) . iso unTagged Tagged -itemTag :: forall l a b s. Had (Tagged l a) s+pieceTag :: forall l a b s. Had (Tagged l a) s => Lens s (Replaced (Tagged l a) (Tagged l b) s) a b-itemTag = item @(Tagged l a) . iso unTagged (Tagged @l)+pieceTag = piece @(Tagged l a) . iso unTagged (Tagged @l) --- | 'grabL' ('view' 'itemL') and 'replaceL' ('set' 'itemL') in 'Lens'' form.+-- | 'grabL' ('view' 'pieceL') and 'replaceL' ('set' 'pieceL') in 'Lens'' form. -- -- @ -- let x = (5 :: Int) './' Tagged \@Foo False './' Tagged \@Bar \'X' './' 'nil'--- x '^.' 'itemL'' \@Foo \`shouldBe` Tagged \@Foo False--- (x '&' 'itemL'' \@Foo '.~' Tagged \@Foo True) \`shouldBe` (5 :: Int) './' Tagged \@Foo True './' Tagged \@Bar \'X' './' 'nil'+-- x '^.' 'pieceL'' \@Foo \`shouldBe` Tagged \@Foo False+-- (x '&' 'pieceL'' \@Foo '.~' Tagged \@Foo True) \`shouldBe` (5 :: Int) './' Tagged \@Foo True './' Tagged \@Bar \'X' './' 'nil' -- @ -- -- A default implementation using generics is not provided as it make GHC think that @l@ must be type @Symbol@ -- when @l@ can actually be any kind. -- Create instances of 'HasL' using "Data.Generics.Product.Fields" as follows: -- @--- instance HasField' l Foo a => itemL' l a Foo where--- itemL' = field @l--- default itemL' :: forall (l :: Symbol) a s. (HasField' l s a) => Lens' s a--- itemL' = field @l+-- instance HasField' l Foo a => pieceL' l a Foo where+-- pieceL' = field @l+-- default pieceL' :: forall (l :: Symbol) a s. (HasField' l s a) => Lens' s a+-- pieceL' = field @l class HasL (l :: k) a s | s l -> a where- itemL' :: Lens' s a+ pieceL' :: Lens' s a instance (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => HasL l x (Many xs) where- itemL' = lens (grabL @l) (replaceL' @l)+ pieceL' = lens (grabL @l) (replaceL' @l) --- | Polymorphic version of 'itemL''+-- | Polymorphic version of 'pieceL'' -- -- @ -- let x = (5 :: Int) './' Tagged @Foo False './' Tagged \@Bar \'X' './' 'nil'--- (x '&' 'itemL' \@Foo '.~' \"foo") \`shouldBe` (5 :: Int) './' \"foo" './' Tagged \@Bar \'X' './' 'nil'+-- (x '&' 'pieceL' \@Foo '.~' \"foo") \`shouldBe` (5 :: Int) './' \"foo" './' Tagged \@Bar \'X' './' 'nil' -- @ class (HasL (l :: k) a s, ReplacedL l a a s ~ s) => HadL (l :: k) a s | s l -> a where type ReplacedL l a b s- itemL :: Lens s (ReplacedL l a b s) a b+ pieceL :: Lens s (ReplacedL l a b s) a b instance (UniqueLabelMember l xs, x ~ KindAtLabel l xs) => HadL l x (Many xs) where type ReplacedL l x b (Many xs) = Many (Replace (KindAtLabel l xs) b xs)- itemL = lens (grabL @l) (replaceL @l)+ pieceL = lens (grabL @l) (replaceL @l) --- | 'grabN' ('view' 'item') and 'replaceN'' ('set' 'item'') in 'Lens'' form.+-- | 'grabN' ('view' 'piece') and 'replaceN'' ('set' 'piece'') in 'Lens'' form. -- -- @ -- let x = (5 :: Int) './' False './' \'X' './' Just \'O' './' (6 :: Int) './' Just \'A' ./ nil--- x '^.' 'itemN'' \@0 \`shouldBe` 5--- (x '&' 'itemN'' \@0 '.~' 6) \`shouldBe` (6 :: Int) './' False './' \'X' './' Just \'O' './' (6 :: Int) './' Just \'A' './' 'nil'+-- x '^.' 'pieceN'' \@0 \`shouldBe` 5+-- (x '&' 'pieceN'' \@0 '.~' 6) \`shouldBe` (6 :: Int) './' False './' \'X' './' Just \'O' './' (6 :: Int) './' Just \'A' './' 'nil' -- @ class HasN (n :: Nat) a s | s n -> a where- itemN' :: Lens' s a+ pieceN' :: Lens' s a instance (MemberAt n x xs) => HasN n x (Many xs) where- itemN' = lens (grabN @n) (replaceN' @n)+ pieceN' = lens (grabN @n) (replaceN' @n) --- | Polymorphic version of 'itemN''+-- | Polymorphic version of 'pieceN'' class (HasN (n :: Nat) a s, ReplacedN n a a s ~ s) => HadN (n :: Nat) a s | s n -> a where type ReplacedN n a b s- itemN :: Lens s (ReplacedN n a b s) a b+ pieceN :: Lens s (ReplacedN n a b s) a b - -- | Make it easy to create an instance of 'itemN' using 'Data.Generics.Product.Positions'- default itemN :: (HasPosition n s (ReplacedN n a b s) a b) => Lens s (ReplacedN n a b s) a b- itemN = position @n+ -- | Make it easy to create an instance of 'pieceN' using 'Data.Generics.Product.Positions'+ default pieceN :: (HasPosition n s (ReplacedN n a b s) a b) => Lens s (ReplacedN n a b s) a b+ pieceN = position @n instance (MemberAt n x xs) => HadN n x (Many xs) where type ReplacedN n x b (Many xs) = Many (ReplaceIndex n x b xs)- itemN = lens (grabN @n) (replaceN @n)+ pieceN = lens (grabN @n) (replaceN @n) -----------------------------------------------------------------------
src/Data/Diverse/Profunctor/Many.hs view
@@ -10,9 +10,9 @@ module Data.Diverse.Profunctor.Many ( -- * Combinators similar to Profunctor Strong- Itemized- , itemized- , itemizedK+ Pieced+ , pieced+ , piecedK , Projected , projected , projectedK@@ -32,30 +32,30 @@ import Data.Diverse.TypeLevel import Data.Profunctor --- | A friendlier constraint synonym for 'itemized'.-type Itemized a b s t =+-- | A friendlier constraint synonym for 'pieced'.+type Pieced a b s t = ( Had a s , t ~ Replaced a b s ) -- | Like 'Strong' or 'Arrow' but lifting into 'Many'-itemized ::+pieced :: forall w a b s t. ( Profunctor w , Strong w- , Itemized a b s t+ , Pieced a b s t ) => w a b -> w s t-itemized w = dimap (\c -> (view item' c, c)) (\(b, c) -> set (item @a) b c) (first' w)+pieced w = dimap (\c -> (view piece' c, c)) (\(b, c) -> set (piece @a) b c) (first' w) --- | 'itemized' under 'Kleisli'-itemizedK ::+-- | 'pieced' under 'Kleisli'+piecedK :: forall m a b s t. ( Monad m- , Itemized a b s t+ , Pieced a b s t ) => (a -> m b) -> (s -> m t)-itemizedK f = runKleisli . itemized $ Kleisli f+piecedK f = runKleisli . pieced $ Kleisli f -- | A friendlier constraint synonym for 'projected'. type Projected a1 a2 b1 b2 =
test/Data/Diverse/Lens/ManySpec.hs view
@@ -32,55 +32,55 @@ x `shouldBe` review _Many' t t `shouldBe` view _Many' x - it "has getter/setter lens using 'item''" $ do+ it "has getter/setter lens using 'piece''" $ do let x = (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ nil- x ^. item' @Int `shouldBe` 5- (x & item' @Int .~ 6) `shouldBe` (6 :: Int) ./ False ./ 'X' ./ Just 'O' ./ nil- x ^. item' @Bool `shouldBe` False- (x & item' @Bool .~ True) `shouldBe` (5 :: Int) ./ True ./ 'X' ./ Just 'O' ./ nil- x ^. item' @Char `shouldBe` 'X'- x ^. item' @(Maybe Char) `shouldBe` Just 'O'+ x ^. piece' @Int `shouldBe` 5+ (x & piece' @Int .~ 6) `shouldBe` (6 :: Int) ./ False ./ 'X' ./ Just 'O' ./ nil+ x ^. piece' @Bool `shouldBe` False+ (x & piece' @Bool .~ True) `shouldBe` (5 :: Int) ./ True ./ 'X' ./ Just 'O' ./ nil+ x ^. piece' @Char `shouldBe` 'X'+ x ^. piece' @(Maybe Char) `shouldBe` Just 'O' - it "has polymorphic getter/setter lens using 'item'" $ do+ it "has polymorphic getter/setter lens using 'piece'" $ do let x = (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ nil- (x & item @(Maybe Char) .~ Just 'P') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'P' ./ nil- (x & item @Int .~ 'Z') `shouldBe` 'Z' ./ False ./ 'X' ./ Just 'O' ./ nil- (x & item @Bool .~ 'Z') `shouldBe` (5 :: Int) ./ 'Z' ./ 'X' ./ Just 'O' ./ nil- (x & item @Char .~ True) `shouldBe` (5 :: Int) ./ False ./ True ./ Just 'O' ./ nil- (x & item @(Maybe Char) .~ 'P') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ 'P' ./ nil+ (x & piece @(Maybe Char) .~ Just 'P') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'P' ./ nil+ (x & piece @Int .~ 'Z') `shouldBe` 'Z' ./ False ./ 'X' ./ Just 'O' ./ nil+ (x & piece @Bool .~ 'Z') `shouldBe` (5 :: Int) ./ 'Z' ./ 'X' ./ Just 'O' ./ nil+ (x & piece @Char .~ True) `shouldBe` (5 :: Int) ./ False ./ True ./ Just 'O' ./ nil+ (x & piece @(Maybe Char) .~ 'P') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ 'P' ./ nil - it "has getter/setter lens using 'itemL''" $ do+ it "has getter/setter lens using 'pieceL''" $ do let x = (5 :: Int) ./ Tagged @Foo False ./ Tagged @Bar 'X' ./ nil- x ^. itemL' @Foo `shouldBe` Tagged @Foo False- (x & itemL' @Foo .~ Tagged @Foo True) `shouldBe` (5 :: Int) ./ Tagged @Foo True ./ Tagged @Bar 'X' ./ nil+ x ^. pieceL' @Foo `shouldBe` Tagged @Foo False+ (x & pieceL' @Foo .~ Tagged @Foo True) `shouldBe` (5 :: Int) ./ Tagged @Foo True ./ Tagged @Bar 'X' ./ nil - it "has polymorphic getter/setter lens using 'itemL'" $ do+ it "has polymorphic getter/setter lens using 'pieceL'" $ do let x = (5 :: Int) ./ Tagged @Foo False ./ Tagged @Bar 'X' ./ nil- (x & itemL @Foo .~ "foo") `shouldBe` (5 :: Int) ./ "foo" ./ Tagged @Bar 'X' ./ nil+ (x & pieceL @Foo .~ "foo") `shouldBe` (5 :: Int) ./ "foo" ./ Tagged @Bar 'X' ./ nil - it "has getter/setter lens for duplicate fields using 'itemN''" $ do+ it "has getter/setter lens for duplicate fields using 'pieceN''" $ do let x = (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- x ^. itemN' @0 `shouldBe` 5- (x & itemN' @0 .~ 6) `shouldBe` (6 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- x ^. itemN' @1 `shouldBe` False- (x & itemN' @1 .~ True) `shouldBe` (5 :: Int) ./ True ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- x ^. itemN' @2 `shouldBe` 'X'- (x & itemN' @2 .~ 'O') `shouldBe` (5 :: Int) ./ False ./ 'O' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- x ^. itemN' @3 `shouldBe` Just 'O'- (x & itemN' @3 .~ Just 'P') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'P' ./ (6 :: Int) ./ Just 'A' ./ nil- x ^. itemN' @4 `shouldBe` 6- (x & itemN' @4 .~ 7) `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (7 :: Int) ./ Just 'A' ./ nil- x ^. itemN' @5 `shouldBe` Just 'A'- (x & itemN' @5 .~ Just 'B') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'B' ./ nil+ x ^. pieceN' @0 `shouldBe` 5+ (x & pieceN' @0 .~ 6) `shouldBe` (6 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil+ x ^. pieceN' @1 `shouldBe` False+ (x & pieceN' @1 .~ True) `shouldBe` (5 :: Int) ./ True ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil+ x ^. pieceN' @2 `shouldBe` 'X'+ (x & pieceN' @2 .~ 'O') `shouldBe` (5 :: Int) ./ False ./ 'O' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil+ x ^. pieceN' @3 `shouldBe` Just 'O'+ (x & pieceN' @3 .~ Just 'P') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'P' ./ (6 :: Int) ./ Just 'A' ./ nil+ x ^. pieceN' @4 `shouldBe` 6+ (x & pieceN' @4 .~ 7) `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (7 :: Int) ./ Just 'A' ./ nil+ x ^. pieceN' @5 `shouldBe` Just 'A'+ (x & pieceN' @5 .~ Just 'B') `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'B' ./ nil - it "has polymorphic getter/setter lens for duplicate fields using 'itemN'" $ do+ it "has polymorphic getter/setter lens for duplicate fields using 'pieceN'" $ do let x = (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- (x & itemN @0 .~ "Foo") `shouldBe` "Foo" ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- (x & itemN @1 .~ "Foo") `shouldBe` (5 :: Int) ./ "Foo" ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- (x & itemN @2 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ "Foo" ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil- (x & itemN @3 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ 'X' ./ "Foo" ./ (6 :: Int) ./ Just 'A' ./ nil- (x & itemN @4 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ "Foo" ./ Just 'A' ./ nil- (x & itemN @5 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ "Foo" ./ nil+ (x & pieceN @0 .~ "Foo") `shouldBe` "Foo" ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil+ (x & pieceN @1 .~ "Foo") `shouldBe` (5 :: Int) ./ "Foo" ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil+ (x & pieceN @2 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ "Foo" ./ Just 'O' ./ (6 :: Int) ./ Just 'A' ./ nil+ (x & pieceN @3 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ 'X' ./ "Foo" ./ (6 :: Int) ./ Just 'A' ./ nil+ (x & pieceN @4 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ "Foo" ./ Just 'A' ./ nil+ (x & pieceN @5 .~ "Foo") `shouldBe` (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ (6 :: Int) ./ "Foo" ./ nil it "has getter/setter lens for multiple fields using 'project''" $ do let x = (5 :: Int) ./ False ./ 'X' ./ Just 'O' ./ nil