non-empty 0.1.1 → 0.1.2
raw patch · 3 files changed
+88/−16 lines, 3 filesdep +containersPVP ok
version bump matches the API change (PVP)
Dependencies added: containers
API changes (from Hackage documentation)
+ Data.NonEmpty.Class: instance Append Seq
+ Data.NonEmpty.Class: instance Cons Seq
+ Data.NonEmpty.Class: instance Empty Seq
+ Data.NonEmpty.Class: instance Empty Set
+ Data.NonEmpty.Class: instance Reverse Seq
+ Data.NonEmpty.Class: instance Singleton Seq
+ Data.NonEmpty.Class: instance Singleton Set
+ Data.NonEmpty.Class: instance Sort Seq
+ Data.NonEmpty.Class: instance View Seq
+ Data.NonEmpty.Class: instance View Set
+ Data.NonEmpty.Class: instance Zip Seq
Files
- non-empty.cabal +16/−8
- src/Data/NonEmpty/Class.hs +40/−1
- src/Data/NonEmptyPrivate.hs +32/−7
non-empty.cabal view
@@ -1,14 +1,14 @@ Name: non-empty-Version: 0.1.1+Version: 0.1.2 License: BSD3 License-File: LICENSE Author: Henning Thielemann <haskell@henning-thielemann.de> Maintainer: Henning Thielemann <haskell@henning-thielemann.de> Homepage: http://code.haskell.org/~thielema/non-empty/ Category: Data-Synopsis: List-like structures with static checks on the number of elements+Synopsis: List-like structures with static restrictions on the number of elements Description:- We provide a data type that allows to store a list-like structure+ We provide the data type @NonEmpty@ that allows to store a list-like structure with at least or exactly @n@ elements, where @n@ is fixed in the type in a kind of Peano encoding and is usually small.@@ -16,8 +16,15 @@ by making functions total that are partial on plain lists. E.g. on a non-empty list, 'head' and 'tail' are always defined. .- The package uses Haskell 98.+ There are more such data types like @Optional@ and @Empty@.+ Together with @NonEmpty@ you can define a list type+ for every finite set of admissible list lengths. .+ The datatype can be combined with Lists, Sequences and Sets+ (from the @containers@ package).+ .+ The package needs only Haskell 98.+ . Similar packages: . * @semigroups@, @semigroupoids@:@@ -50,7 +57,7 @@ Build-Type: Simple Source-Repository this- Tag: 0.1.1+ Tag: 0.1.2 Type: darcs Location: http://code.haskell.org/~thielema/non-empty @@ -60,9 +67,10 @@ Library Build-Depends:- utility-ht >= 0.0.1 && <0.1,- QuickCheck >= 2.1 && <3- Build-Depends: base >= 4 && < 5+ containers >=0.4 && <0.6,+ utility-ht >=0.0.1 && <0.1,+ QuickCheck >=2.1 && <3,+ base >=4 && <5 GHC-Options: -Wall Hs-Source-Dirs: src
src/Data/NonEmpty/Class.hs view
@@ -1,7 +1,11 @@ module Data.NonEmpty.Class where +import qualified Data.Sequence as Seq+import qualified Data.Set as Set import qualified Data.List.HT as ListHT import qualified Data.List as List+import Data.Sequence (Seq, )+import Data.Set (Set, ) import Control.Monad (liftM2, ) import qualified Test.QuickCheck as QC@@ -19,14 +23,23 @@ instance Empty Maybe where empty = Nothing +instance Empty Set where+ empty = Set.empty +instance Empty Seq where+ empty = Seq.empty++ class Cons f where cons :: a -> f a -> f a instance Cons [] where cons = (:) +instance Cons Seq where+ cons = (Seq.<|) + class View f where viewL :: f a -> Maybe (a, f a) @@ -36,7 +49,17 @@ instance View Maybe where viewL = fmap (\a -> (a, Nothing)) +instance View Set where+ viewL = Set.minView +instance View Seq where+ viewL x =+ case Seq.viewl x of+ Seq.EmptyL -> Nothing+ y Seq.:< ys -> Just (y,ys)+ -- viewL x = do y Seq.:< ys <- Just $ Seq.viewl x; Just (y,ys)++ class Singleton f where singleton :: a -> f a @@ -46,13 +69,22 @@ instance Singleton Maybe where singleton x = Just x +instance Singleton Set where+ singleton = Set.singleton +instance Singleton Seq where+ singleton = Seq.singleton++ class Append f where append :: f a -> f a -> f a instance Append [] where append = (++) +instance Append Seq where+ append = (Seq.><)+ infixr 5 `cons`, `append` @@ -72,6 +104,9 @@ instance Zip Maybe where zipWith = liftM2 +instance Zip Seq where+ zipWith = Seq.zipWith+ zip :: (Zip f) => f a -> f b -> f (a,b) zip = zipWith (,) @@ -92,6 +127,9 @@ instance Sort Maybe where sortBy _f = id +instance Sort Seq where+ sortBy = Seq.sortBy+ sort :: (Ord a, Sort f) => f a -> f a sort = sortBy compare @@ -99,8 +137,9 @@ class Reverse f where reverse :: f a -> f a -instance Reverse [] where reverse = P.reverse+instance Reverse [] where reverse = List.reverse instance Reverse Maybe where reverse = id+instance Reverse Seq where reverse = Seq.reverse class Show f where
src/Data/NonEmptyPrivate.hs view
@@ -3,6 +3,9 @@ import qualified Data.NonEmpty.Class as C import qualified Data.Empty as Empty +import qualified Data.Sequence as Seq+import Data.Sequence (Seq, )+ import qualified Data.Traversable as Trav import qualified Data.Foldable as Fold import qualified Data.List.HT as ListHT@@ -16,6 +19,7 @@ import Data.Function (flip, const, ($), (.), ) import Data.Maybe (Maybe(Just, Nothing), maybe, mapMaybe, ) import Data.Ord (Ord, Ordering(GT), (<), (>), compare, comparing, )+import Data.Eq ((==), ) import Data.Tuple.HT (mapSnd, ) import Data.Tuple (fst, snd, ) import qualified Prelude as P@@ -355,7 +359,20 @@ GT -> (x, y) _ -> (y, x) +instance Insert Seq where+ {-+ If we assume a sorted list+ we could do binary search for the splitting point.+ -}+ insertBy f y xt =+ uncurry Cons $+ case Seq.spanl ((GT ==) . f y) xt of+ (ys,zs) ->+ case Seq.viewl ys of+ Seq.EmptyL -> (y, xt)+ w Seq.:< ws -> (w, ws Seq.>< y Seq.<| zs) + {- | Insert an element into an ordered list while preserving the order. The first element of the resulting list is returned individually.@@ -396,13 +413,7 @@ tails :: (C.Cons g, C.Empty g) => f a -> T f (g a) instance Tails [] where- tails xt =- force $- case C.viewL xt of- Nothing -> Cons C.empty C.empty- Just (x, xs) ->- case tails xs of- xss -> cons (C.cons x $ head xss) xss+ tails = tailsDefault instance Tails Empty.T where tails Empty.Cons = Cons C.empty Empty.Cons@@ -419,6 +430,20 @@ Nothing -> Cons C.empty Nothing Just x -> Cons (C.cons x C.empty) (Just C.empty) +instance Tails Seq where+ tails = tailsDefault++tailsDefault ::+ (C.Cons f, C.Empty f, C.View f, Tails f,+ C.Cons g, C.Empty g) =>+ f a -> T f (g a)+tailsDefault xt =+ force $+ case C.viewL xt of+ Nothing -> Cons C.empty C.empty+ Just (x, xs) ->+ case tails xs of+ xss -> cons (C.cons x $ head xss) xss newtype Zip f a = Zip {unZip :: f a}