packages feed

non-empty-containers (empty) → 0.1.0.0

raw patch · 5 files changed

+122/−0 lines, 5 filesdep +basedep +containerssetup-changed

Dependencies added: base, containers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Andrew Martin (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 Andrew Martin 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,1 @@+# non-empty-containers
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ non-empty-containers.cabal view
@@ -0,0 +1,28 @@+cabal-version: 2.0+name: non-empty-containers+version: 0.1.0.0+description: Please see the README on GitHub at <https://github.com/andrewthad/non-empty-containers#readme>+homepage: https://github.com/andrewthad/non-empty-containers#readme+bug-reports: https://github.com/andrewthad/non-empty-containers/issues+author: Andrew Martin+maintainer: andrew.thaddeus@gmail.com+copyright: 2018 Andrew Martin+license: BSD3+license-file: LICENSE+build-type: Simple+extra-source-files: README.md++source-repository head+  type: git+  location: https://github.com/andrewthad/non-empty-containers++library+  exposed-modules:+    Data.Set.NonEmpty+  hs-source-dirs: src+  build-depends:+      base >=4.11 && <5+    , containers >= 0.5.10 && < 0.7+  ghc-options: -O2 -Wall+  default-language: Haskell2010+
+ src/Data/Set/NonEmpty.hs view
@@ -0,0 +1,61 @@+module Data.Set.NonEmpty+  ( NonEmptySet+  , singleton+  , toNonEmpty+  , fromNonEmpty+  ) where++import Data.Foldable+import Data.List.NonEmpty+import qualified Data.Set as S++-- | A non-empty set.+data NonEmptySet a = NonEmptySet !a !(S.Set a)+  deriving (Eq,Ord)++-- The internal invariant for a NonEmptySet is that the first+-- element in the NonEmptySet data constructor must be less+-- than everything in the Set that is the second argument.++-- | Create a non-empty set with a single element.+singleton :: a -> NonEmptySet a+singleton x = NonEmptySet x S.empty++-- | Convert a non-empty set to a non-empty list.+toNonEmpty :: NonEmptySet a -> NonEmpty a+toNonEmpty (NonEmptySet x xs) = x :| S.toList xs++-- | Create a non-empty set from a non-empty list.+fromNonEmpty :: Ord a => NonEmpty a -> NonEmptySet a+fromNonEmpty (x :| xs) = case S.minView s of+  Nothing -> NonEmptySet x S.empty+  Just (m,s') -> case compare x m of+    EQ -> NonEmptySet m s'+    GT -> NonEmptySet m (S.insert x s')+    LT -> NonEmptySet x s+  where+  s = S.fromList xs++instance Show a => Show (NonEmptySet a) where+  showsPrec p xs = showParen (p > 10) $+    showString "fromNonEmpty " . shows (toNonEmpty xs)++instance Foldable NonEmptySet where+  fold (NonEmptySet a s) = a <> fold s+  foldMap f (NonEmptySet a s) = f a <> foldMap f s+  foldl1 f (NonEmptySet a s) = S.foldl f a s+  foldr1 f (NonEmptySet a s) = case S.maxView s of+    Nothing -> a+    Just (m,s') -> f a (S.foldr f m s')+  minimum (NonEmptySet a _) = a+  maximum (NonEmptySet a s) = case S.lookupMax s of+    Nothing -> a+    Just m -> m+  length (NonEmptySet _ s) = 1 + S.size s++instance Ord a => Semigroup (NonEmptySet a) where+  NonEmptySet x xs <> NonEmptySet y ys = case compare x y of+    EQ -> NonEmptySet x (xs <> ys)+    LT -> NonEmptySet x (xs <> S.insert y ys)+    GT -> NonEmptySet y (S.insert x xs <> ys)+