packages feed

ixset-typed-conversions (empty) → 0.1.0.0

raw patch · 6 files changed

+128/−0 lines, 6 filesdep +basedep +exceptionsdep +hashablesetup-changed

Dependencies added: base, exceptions, hashable, ixset-typed, unordered-containers, zipper-extra

Files

+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Changelog for ixset-typed-conversions++## (v0.1.0.0)++Add conversion functions from `IxSet` to `HashMap` and `Zipper []`.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2020++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 Author name here 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.
+ README.md view
@@ -0,0 +1,3 @@+# ixset-typed-conversions++Conversions from ixset-typed to other containers.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ ixset-typed-conversions.cabal view
@@ -0,0 +1,41 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.2.+--+-- see: https://github.com/sol/hpack++name:           ixset-typed-conversions+version:        0.1.0.0+synopsis:       Conversions from ixset-typed to other containers.+description:    Conversions from ixset-typed to other containers; HashMaps, zippers.+category:       Data Structures+author:         Daniel Firth+maintainer:     dan.firth@homotopic.tech+copyright:      2020 Daniel Firth+license:        MIT+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    README.md+    ChangeLog.md++source-repository head+  type: git+  location: https://gitlab.com/homotopic-tech/ixset-typed-conversions++library+  exposed-modules:+      Data.IxSet.Typed.Conversions+  other-modules:+      Paths_ixset_typed_conversions+  hs-source-dirs:+      src+  ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wredundant-constraints+  build-depends:+      base >=4.7 && <5+    , exceptions+    , hashable+    , ixset-typed+    , unordered-containers+    , zipper-extra+  default-language: Haskell2010
+ src/Data/IxSet/Typed/Conversions.hs view
@@ -0,0 +1,47 @@+{-# LANGUAGE ScopedTypeVariables #-}+{- |+   Module     : Data.IxSet.Typed.Conversions+   Copyright  : Copyright (C) 2020 Daniel Firth+   Maintainer : Daniel Firth <dan.firth@homotopic.tech+   License    : MIT+   Stability  : experimental++Conversions from ixset-typed to other containers.+-}+module Data.IxSet.Typed.Conversions (+  toHashMap+, toHashMapBy+, toHashMapByM+, toZipperAsc+, toZipperDesc+) where++import           Control.Comonad.Zipper.Extra+import           Control.Monad+import           Control.Monad.Catch+import           Data.Hashable+import qualified Data.HashMap.Strict          as HM+import           Data.IxSet.Typed             as Ix+import           Data.Proxy++-- | Convert an `IxSet to a `HashMap`.+toHashMap :: (Hashable a, IsIndexOf a xs) => IxSet xs k -> HM.HashMap a [k]+toHashMap = HM.fromList . Ix.groupDescBy++-- | Convert an `IxSet` to a `HashMap` via a function on the index and associated list.+toHashMapBy :: (Hashable a, IsIndexOf a xs) => IxSet xs k -> (a -> [k] -> k') -> HM.HashMap a k'+toHashMapBy xs f = HM.fromList $ flip map (Ix.groupDescBy xs) $ \(a, ks) -> (a, f a ks)++-- | Monadic variant of `toHashMapBy`.+toHashMapByM :: (Monad m, Hashable a, IsIndexOf a xs) => IxSet xs k -> (a -> [k] -> m k') -> m (HM.HashMap a k')+toHashMapByM xs f = fmap HM.fromList $ forM (Ix.groupDescBy xs) $ \(a, ks) -> do+                       z <- f a ks+                       return (a, z)++-- | Convert an `IxSet` to a `Zipper` by descending sort on an index.+toZipperAsc :: forall proxy ix ixs a m. (IsIndexOf ix ixs, MonadThrow m) => proxy ix -> IxSet ixs a -> m (Zipper [] a)+toZipperAsc _ = zipper' . Ix.toAscList (Proxy :: Proxy ix)++-- | Convert an `IxSet` to a `Zipper` by descending sort on an index.+toZipperDesc :: forall proxy ix ixs a m. (IsIndexOf ix ixs, MonadThrow m) => proxy ix -> IxSet ixs a -> m (Zipper [] a)+toZipperDesc _ = zipper' . Ix.toDescList (Proxy :: Proxy ix)