packages feed

insert-ordered-containers-optics (empty) → 0.1

raw patch · 6 files changed

+264/−0 lines, 6 filesdep +basedep +hashabledep +insert-ordered-containers

Dependencies added: base, hashable, insert-ordered-containers, insert-ordered-containers-optics, optics-core, tasty, tasty-quickcheck, unordered-containers

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+- 0.1+    - Initial release: the `optics` instances split out of+      `insert-ordered-containers-0.3`, plus `hashMap`, `unorderedTraversal`+      and `hashSet` optics.+      https://github.com/erikd/insert-ordered-containers/issues/17
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Oleg Grenrus++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Oleg Grenrus nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ insert-ordered-containers-optics.cabal view
@@ -0,0 +1,67 @@+cabal-version:      2.2+name:               insert-ordered-containers-optics+version:            0.1+synopsis:           optics support for insert-ordered-containers+description:+  @optics@ support for the containers from+  <https://hackage.haskell.org/package/insert-ordered-containers insert-ordered-containers>,+  including orphan instances.++category:           Data, Optics+homepage:           https://github.com/erikd/insert-ordered-containers#readme+bug-reports:        https://github.com/erikd/insert-ordered-containers/issues+author:             Oleg Grenrus <oleg.grenrus@iki.fi>+maintainer:         Erik de Castro Lopo <erikd@mega-nerd.com>, Oleg Grenrus <oleg.grenrus@iki.fi>+license:            BSD-3-Clause+license-file:       LICENSE+build-type:         Simple+tested-with:+  GHC ==8.6.5+   || ==8.8.4+   || ==8.10.7+   || ==9.0.2+   || ==9.2.8+   || ==9.4.8+   || ==9.6.7+   || ==9.8.4+   || ==9.10.3+   || ==9.12.2+   || ==9.14.1++extra-source-files:+  CHANGELOG.md++source-repository head+  type:     git+  location: https://github.com/erikd/insert-ordered-containers+  subdir:   insert-ordered-containers-optics++library+  default-language: Haskell2010+  hs-source-dirs:   src+  ghc-options:      -Wall -Wunused-packages+  build-depends:+    , base                       >=4.12.0.0 && <4.23+    , hashable                   >=1.4.7.0  && <1.6+    , insert-ordered-containers  >=0.4      && <0.5+    , optics-core                >=0.4.1.1  && <0.5+    , unordered-containers       >=0.2.20   && <0.3++  exposed-modules:+    Data.HashMap.Strict.InsOrd.Optics+    Data.HashSet.InsOrd.Optics++test-suite ins-ord-containers-optics-tests+  default-language: Haskell2010+  type:             exitcode-stdio-1.0+  main-is:          Tests.hs+  hs-source-dirs:   test+  ghc-options:      -Wall++  build-depends:+    , base+    , insert-ordered-containers+    , insert-ordered-containers-optics+    , optics-core+    , tasty                             >=0.10.1.2 && <1.6+    , tasty-quickcheck                  >=0.8.3.2  && <0.12
+ src/Data/HashMap/Strict/InsOrd/Optics.hs view
@@ -0,0 +1,42 @@+{-# LANGUAGE TypeFamilies #-}+{-# OPTIONS_GHC -Wno-orphans #-}+-- | @optics@ support for 'InsOrdHashMap'.+module Data.HashMap.Strict.InsOrd.Optics (+    hashMap,+    unorderedTraversal,+    ) where++import Data.Functor  ((<&>))+import Data.Hashable (Hashable)+import Optics.Core+       (At (..), Index, Iso, IxValue, Ixed (..), Traversal, atraversalVL, iso,+       lensVL, traversed, (%))++import           Data.HashMap.Strict        (HashMap)+import           Data.HashMap.Strict.InsOrd (InsOrdHashMap)+import qualified Data.HashMap.Strict.InsOrd as InsOrd++type instance Index (InsOrdHashMap k v) = k+type instance IxValue (InsOrdHashMap k v) = v++instance (Eq k, Hashable k) => Ixed (InsOrdHashMap k v) where+    ix k = atraversalVL $ \point f m -> case InsOrd.lookup k m of+        Just v  -> f v <&> \v' -> InsOrd.insert k v' m+        Nothing -> point m+    {-# INLINE ix #-}++instance (Eq k, Hashable k) => At (InsOrdHashMap k v) where+    at k = lensVL $ \f m ->+        let mv = InsOrd.lookup k m+        in f mv <&> \r -> case r of+            Nothing -> maybe m (const (InsOrd.delete k m)) mv+            Just v' -> InsOrd.insert k v' m+    {-# INLINE at #-}++-- | An isomorphism only up to insertion order: converting to a 'HashMap' and+-- back replaces the insertion order with the unspecified order of the 'HashMap'.+hashMap :: Iso (InsOrdHashMap k a) (InsOrdHashMap k b) (HashMap k a) (HashMap k b)+hashMap = iso InsOrd.toHashMap InsOrd.fromHashMap++unorderedTraversal :: Traversal (InsOrdHashMap k a) (InsOrdHashMap k b) a b+unorderedTraversal = hashMap % traversed
+ src/Data/HashSet/InsOrd/Optics.hs view
@@ -0,0 +1,42 @@+{-# LANGUAGE TypeFamilies #-}+{-# OPTIONS_GHC -Wno-orphans #-}+-- | @optics@ support for 'InsOrdHashSet'.+module Data.HashSet.InsOrd.Optics (+    hashSet,+    ) where++import Data.Functor  ((<&>))+import Data.Hashable (Hashable)+import Optics.Core+       (At (..), Contains (..), Index, Iso', IxValue, Ixed (..), atraversalVL,+       iso, lensVL)++import           Data.HashSet        (HashSet)+import           Data.HashSet.InsOrd (InsOrdHashSet)+import qualified Data.HashSet.InsOrd as InsOrd++type instance Index (InsOrdHashSet a) = a+type instance IxValue (InsOrdHashSet a) = ()++instance (Eq k, Hashable k) => Ixed (InsOrdHashSet k) where+    ix k = atraversalVL $ \point f s ->+        if InsOrd.member k s then s <$ f () else point s+    {-# INLINE ix #-}++instance (Eq k, Hashable k) => At (InsOrdHashSet k) where+    at k = lensVL $ \f s ->+        let mv = if InsOrd.member k s then Just () else Nothing+        in f mv <&> \r -> case r of+            Nothing -> maybe s (const (InsOrd.delete k s)) mv+            Just () -> InsOrd.insert k s+    {-# INLINE at #-}++instance (Eq a, Hashable a) => Contains (InsOrdHashSet a) where+    contains k = lensVL $ \f s -> f (InsOrd.member k s) <&> \b ->+        if b then InsOrd.insert k s else InsOrd.delete k s+    {-# INLINE contains #-}++-- | An isomorphism only up to insertion order: converting to a 'HashSet' and+-- back replaces the insertion order with the unspecified order of the 'HashSet'.+hashSet :: Iso' (InsOrdHashSet a) (HashSet a)+hashSet = iso InsOrd.toHashSet InsOrd.fromHashSet
+ test/Tests.hs view
@@ -0,0 +1,78 @@+module Main (main) where++import Data.Function (on)+import Data.List     (sort)+import Optics.Core++import qualified Data.HashMap.Strict.InsOrd as InsOrd+import qualified Data.HashSet.InsOrd        as InsOrdSet++import Data.HashMap.Strict.InsOrd.Optics+import Data.HashSet.InsOrd.Optics++import Test.Tasty+import Test.Tasty.QuickCheck++type M = InsOrd.InsOrdHashMap Int Int+type S = InsOrdSet.InsOrdHashSet Int++main :: IO ()+main = defaultMain $ testGroup "insert-ordered-containers-optics"+    [ testGroup "InsOrdHashMap"+        [ testProperty "view at = lookup" $ \kvs k ->+            let m = InsOrd.fromList kvs :: M+            in view (at k) m === InsOrd.lookup k m+        , testProperty "set at (Just v) = insert" $ \kvs k v ->+            let m = InsOrd.fromList kvs :: M+            in (m & at k ?~ v) === InsOrd.insert k v m+        , testProperty "set at Nothing = delete" $ \kvs k ->+            let m = InsOrd.fromList kvs :: M+            in (m & at k .~ Nothing) === InsOrd.delete k m+        , testProperty "preview ix = lookup" $ \kvs k ->+            let m = InsOrd.fromList kvs :: M+            in preview (ix k) m === InsOrd.lookup k m+        , testProperty "over ix = adjust" $ \kvs k f ->+            let m = InsOrd.fromList kvs :: M+            in (m & ix k %~ applyFun f) === InsOrd.adjust (applyFun f) k m+        , testProperty "view hashMap = toHashMap" $ \kvs ->+            let m = InsOrd.fromList kvs :: M+            in view hashMap m === InsOrd.toHashMap m+        , testProperty "unorderedTraversal visits every element" $ \kvs ->+            let m = InsOrd.fromList kvs :: M+            in ((===) `on` sort) (toListOf unorderedTraversal m) (InsOrd.elems m)+        ]+    , testGroup "InsOrdHashSet"+        [ testProperty "view contains = member" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in view (contains k) s === InsOrdSet.member k s+        , testProperty "set contains True = insert for absent k" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in not (InsOrdSet.member k s) ==>+               (s & contains k .~ True) === InsOrdSet.insert k s+        , testProperty "set contains (view contains) = id" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in (s & contains k .~ view (contains k) s) === s+        , testProperty "set contains False = delete" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in (s & contains k .~ False) === InsOrdSet.delete k s+        , testProperty "view at = member" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in view (at k) s === (if InsOrdSet.member k s then Just () else Nothing)+        , testProperty "set at (Just ()) = insert for absent k" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in not (InsOrdSet.member k s) ==>+               (s & at k ?~ ()) === InsOrdSet.insert k s+        , testProperty "set at (view at) = id" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in (s & at k .~ view (at k) s) === s+        , testProperty "set at Nothing = delete" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in (s & at k .~ Nothing) === InsOrdSet.delete k s+        , testProperty "has ix = member" $ \ks k ->+            let s = InsOrdSet.fromList ks :: S+            in has (ix k) s === InsOrdSet.member k s+        , testProperty "view hashSet = toHashSet" $ \ks ->+            let s = InsOrdSet.fromList ks :: S+            in view hashSet s === InsOrdSet.toHashSet s+        ]+    ]