nonemptymap (empty) → 0.0.1.0
raw patch · 5 files changed
+200/−0 lines, 5 filesdep +basedep +containerssetup-changed
Dependencies added: base, containers
Files
- LICENSE +30/−0
- README.md +5/−0
- Setup.hs +5/−0
- nonemptymap.cabal +27/−0
- src/Data/Map/NonEmpty.hs +133/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Christopher Davenport (c) 2018++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 Christopher Davenport 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,5 @@+# Data.Map.NonEmpty - NonEmptyMap [](https://travis-ci.com/ChristopherDavenport/NonEmptyMap)++This map implementation is a NonEmptyMap that can be used where that is necessary. Hopefully slowly porting over more and more functions from the containers libraries.++There is a lot more work to be done, so please feel free to help get this operational.
+ Setup.hs view
@@ -0,0 +1,5 @@+module Main (main) where+import Distribution.Simple++main :: IO ()+main = defaultMain
+ nonemptymap.cabal view
@@ -0,0 +1,27 @@+name: nonemptymap+version: 0.0.1.0+synopsis: A NonEmptyMap Implementation+description: This package intends to allow general use of a NonEmptyMap+ which is very beneficial as sometimes you want the functionality+ this provides.+homepage: https://github.com/ChristopherDavenport/nonemptymap#readme+license: BSD3+license-file: LICENSE+author: Christopher Davenport+maintainer: Chris@ChristopherDavenport.tech+copyright: 2018 Christopher Davenport+category: Data Structures+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Data.Map.NonEmpty+ build-depends: base >= 4.7 && < 5+ , containers >= 0.5.8 && < 0.6+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/ChristopherDavenport/nonemptymap
+ src/Data/Map/NonEmpty.hs view
@@ -0,0 +1,133 @@+{-# language InstanceSigs #-}+{-# language ScopedTypeVariables #-}++module Data.Map.NonEmpty(+ NonEmptyMap(..) -- Generic Constructor+ -- * Construction+ , singleton -- :: (k, a) -> NonEmptyMap k v+ , fromList -- :: Ord k => [(k, a)] -> Maybe (NonEmptyMap k a)+ -- * Insertion+ , insert -- :: Ord k => k -> a -> NonEmptyMap k a -> NonEmptyMap k a+ , insertWith -- :: Ord k => (a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a+ , insertWithKey -- :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a+ , insertLookupWithKey -- :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> (Maybe a, NonEmptyMap k a)+ -- * Deletion/Update+ , delete -- :: Ord k => k -> NonEmptyMap k a -> Map.Map k a+ , adjust -- :: Ord k => (a -> a) -> k -> NonEmptyMap k a -> NonEmptyMap k a + , update -- :: Ord k => (a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a+ , alter -- :: Ord k => (Maybe a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a+ , alterF -- :: forall f k a. (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> NonEmptyMap k a -> f (Map.Map k a)+ -- * Query+ , lookup -- :: Ord k => k -> NonEmptyMap k a -> Maybe a+ , (!?) -- :: Ord k => NonEmptyMap k a -> k -> Maybe a+ , findWithDefault -- :: Ord k => a -> k -> NonEmptyMap k a -> a+ , member -- :: Ord k => k -> NonEmptyMap k a -> Bool+ , notMember -- :: Ord k => k -> NonEmptyMap k a -> Bool+ -- * Size + , size -- :: NonEmptyMap k a -> In+ -- * Conversions+ -- * Lists+ , toList -- :: NonEmptyMap k a -> [(k, a)]+) where++import qualified Data.Map as Map+import Data.Maybe (fromMaybe, isJust)+import Prelude hiding (lookup)+++-- A NonEmptyMap of keys k to values a+data NonEmptyMap k a = NonEmptyMap (k, a) (Map.Map k a)++-- Instances++instance Functor (NonEmptyMap k) where+ fmap :: (a -> b) -> NonEmptyMap k a -> NonEmptyMap k b+ fmap f (NonEmptyMap (k, v) map) = NonEmptyMap (k, f v) (fmap f map)++-- Construction+singleton :: (k, a) -> NonEmptyMap k a+singleton tup = NonEmptyMap tup Map.empty++fromList :: Ord k => [(k, a)] -> Maybe (NonEmptyMap k a)+fromList [] = Nothing+fromList (x : xa) = Just $ NonEmptyMap x (Map.fromList xa)++-- Insertion++-- , insert+insert :: Ord k => k -> a -> NonEmptyMap k a -> NonEmptyMap k a+insert = insertWith const++-- , insertWith+insertWith :: Ord k => (a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a+insertWith f key value (NonEmptyMap (k, a) m) | key == k = NonEmptyMap (key, f value a) m+insertWith f key value (NonEmptyMap (k, a) m) = NonEmptyMap (k, a) (Map.insertWith f key value m)+-- , insertWithKey+insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> NonEmptyMap k a+insertWithKey f key value (NonEmptyMap (k, a) m) = + if k == key then NonEmptyMap (key, f key value a) m+ else NonEmptyMap (k, a) (Map.insertWithKey f key value m)++-- insertLookupWithKey+insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> NonEmptyMap k a -> (Maybe a, NonEmptyMap k a)+insertLookupWithKey f key value (NonEmptyMap (k, a) m) = + if k == key then (Just a, NonEmptyMap(key, f key value a) m)+ else fmap (NonEmptyMap (k, a)) (Map.insertLookupWithKey f key value m)++-- Deletion/Update+delete :: Ord k => k -> NonEmptyMap k a -> Map.Map k a+delete key (NonEmptyMap (k, a) m) | key == k = m+delete key (NonEmptyMap (k, a) m) = Map.insert k a (Map.delete k m)++adjust :: Ord k => (a -> a) -> k -> NonEmptyMap k a -> NonEmptyMap k a +adjust f key (NonEmptyMap (k, a) m) | key == k = NonEmptyMap (key, f a) m+adjust f key (NonEmptyMap (k, a) m) = NonEmptyMap (k, a) (Map.adjust f key m)++update :: Ord k => (a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a+update f key (NonEmptyMap (k, a) m) | key == k = case f a of+ Just a -> Map.insert k a m+ Nothing -> m+update f key (NonEmptyMap (k, a) m) = Map.insert k a (Map.update f key m)++alter :: Ord k => (Maybe a -> Maybe a) -> k -> NonEmptyMap k a -> Map.Map k a+alter f key (NonEmptyMap (k, a) m) | key == k = case f (Just a) of+ Just a -> Map.insert k a m+ Nothing -> m+alter f key (NonEmptyMap (k, a) m) = Map.insert k a (Map.alter f key m)++alterF :: forall f k a. (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> NonEmptyMap k a -> f (Map.Map k a) +alterF f key (NonEmptyMap (k, a) m) | key == k = insideF <$> f (Just a)+ where+ insideF :: Maybe a -> Map.Map k a+ insideF (Just a) = Map.insert k a m+ insideF Nothing = m+alterF f key (NonEmptyMap (k, a) m) = Map.insert k a <$> Map.alterF f key m++-- Query++lookup :: Ord k => k -> NonEmptyMap k a -> Maybe a+lookup key (NonEmptyMap (k, a) m) | key == k = Just a+lookup key (NonEmptyMap _ m) = Map.lookup key m++(!?) :: Ord k => NonEmptyMap k a -> k -> Maybe a+(!?) nem k = lookup k nem++findWithDefault :: Ord k => a -> k -> NonEmptyMap k a -> a+findWithDefault a key nem = fromMaybe a (lookup key nem)++member :: Ord k => k -> NonEmptyMap k a -> Bool+member key nem = isJust (lookup key nem)++notMember :: Ord k => k -> NonEmptyMap k a -> Bool+notMember k nem = not $ member k nem++-- Size +size :: NonEmptyMap k a -> Int+size (NonEmptyMap _ m) = 1 + Map.size m ++-- Conversions+++-- Lists+toList :: NonEmptyMap k a -> [(k, a)]+toList (NonEmptyMap tup m) = tup : Map.toList m