insert-ordered-containers-lens (empty) → 0.1
raw patch · 6 files changed
+259/−0 lines, 6 filesdep +basedep +hashabledep +insert-ordered-containers
Dependencies added: base, hashable, insert-ordered-containers, insert-ordered-containers-lens, lens, tasty, tasty-quickcheck, unordered-containers
Files
- CHANGELOG.md +4/−0
- LICENSE +30/−0
- insert-ordered-containers-lens.cabal +67/−0
- src/Data/HashMap/Strict/InsOrd/Lens.hs +40/−0
- src/Data/HashSet/InsOrd/Lens.hs +41/−0
- test/Tests.hs +77/−0
+ CHANGELOG.md view
@@ -0,0 +1,4 @@+- 0.1+ - Initial release: the `lens` instances and optics split out of+ `insert-ordered-containers-0.3`.+ 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-lens.cabal view
@@ -0,0 +1,67 @@+cabal-version: 2.2+name: insert-ordered-containers-lens+version: 0.1+synopsis: lens support for insert-ordered-containers+description:+ @lens@ support for the containers from+ <https://hackage.haskell.org/package/insert-ordered-containers insert-ordered-containers>,+ including orphan instances.++category: Data, Lenses+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-lens++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+ , lens >=5.2.3 && <5.4+ , unordered-containers >=0.2.20 && <0.3++ exposed-modules:+ Data.HashMap.Strict.InsOrd.Lens+ Data.HashSet.InsOrd.Lens++test-suite ins-ord-containers-lens-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-lens+ , lens+ , tasty >=0.10.1.2 && <1.6+ , tasty-quickcheck >=0.8.3.2 && <0.12
+ src/Data/HashMap/Strict/InsOrd/Lens.hs view
@@ -0,0 +1,40 @@+{-# LANGUAGE TypeFamilies #-}+{-# OPTIONS_GHC -Wno-orphans #-}+-- | @lens@ support for 'InsOrdHashMap'.+module Data.HashMap.Strict.InsOrd.Lens (+ hashMap,+ unorderedTraversal,+ ) where++import Control.Lens+ (At (..), Index, Iso, IxValue, Ixed (..), Traversal, iso)+import Data.Functor ((<&>))+import Data.Hashable (Hashable)++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 f m = case InsOrd.lookup k m of+ Just v -> f v <&> \v' -> InsOrd.insert k v' m+ Nothing -> pure m+ {-# INLINABLE ix #-}++instance (Eq k, Hashable k) => At (InsOrdHashMap k v) where+ at k f m = f mv <&> \r -> case r of+ Nothing -> maybe m (const (InsOrd.delete k m)) mv+ Just v' -> InsOrd.insert k v' m+ where mv = InsOrd.lookup k m+ {-# INLINABLE 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 . traverse
+ src/Data/HashSet/InsOrd/Lens.hs view
@@ -0,0 +1,41 @@+{-# LANGUAGE TypeFamilies #-}+{-# OPTIONS_GHC -Wno-orphans #-}+-- | @lens@ support for 'InsOrdHashSet'.+module Data.HashSet.InsOrd.Lens (+ hashSet,+ ) where++import Control.Lens+ (At (..), Contains (..), Index, Iso', IxValue, Ixed (..), iso)+import Data.Functor ((<&>))+import Data.Hashable (Hashable)++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 f s+ | InsOrd.member k s = s <$ f ()+ | otherwise = pure s+ {-# INLINE ix #-}++instance (Eq k, Hashable k) => At (InsOrdHashSet k) where+ at k f s = f mv <&> \r -> case r of+ Nothing -> maybe s (const (InsOrd.delete k s)) mv+ Just () -> InsOrd.insert k s+ where mv = if InsOrd.member k s then Just () else Nothing+ {-# INLINE at #-}++instance (Eq a, Hashable a) => Contains (InsOrdHashSet a) where+ contains k 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,77 @@+module Main (main) where++import Control.Lens+import Data.List (sort)++import qualified Data.HashMap.Strict.InsOrd as InsOrd+import qualified Data.HashSet.InsOrd as InsOrdSet++import Data.HashMap.Strict.InsOrd.Lens+import Data.HashSet.InsOrd.Lens++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-lens"+ [ testGroup "InsOrdHashMap"+ [ testProperty "view at = lookup" $ \kvs k ->+ let m = InsOrd.fromList kvs :: M+ in m ^. at k === 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 m ^? ix k === 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 m ^. hashMap === InsOrd.toHashMap m+ , testProperty "unorderedTraversal visits every element" $ \kvs ->+ let m = InsOrd.fromList kvs :: M+ in sort (m ^.. unorderedTraversal) === sort (InsOrd.elems m)+ ]+ , testGroup "InsOrdHashSet"+ [ testProperty "view contains = member" $ \ks k ->+ let s = InsOrdSet.fromList ks :: S+ in s ^. contains k === 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 .~ (s ^. contains k)) === 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 s ^. at k === (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 .~ (s ^. at k)) === 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 s ^. hashSet === InsOrdSet.toHashSet s+ ]+ ]