dstring 0.3.0.1 → 0.3.0.2
raw patch · 3 files changed
+109/−76 lines, 3 filesdep +base-unicode-symbolsdep ~basePVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: base-unicode-symbols
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Data.DString: instance Typeable DString
- Data.DString: foldr :: (Char -> b -> b) -> b -> DString -> b
+ Data.DString: foldr :: (Char -> α -> α) -> α -> DString -> α
- Data.DString: list :: b -> (Char -> DString -> b) -> DString -> b
+ Data.DString: list :: α -> (Char -> DString -> α) -> DString -> α
- Data.DString: unfoldr :: (b -> Maybe (Char, b)) -> b -> DString
+ Data.DString: unfoldr :: (α -> Maybe (Char, α)) -> α -> DString
Files
- Data/DString.hs +96/−64
- LICENSE +1/−1
- dstring.cabal +12/−11
Data/DString.hs view
@@ -1,4 +1,8 @@-{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE UnicodeSyntax+ , NoImplicitPrelude+ , GeneralizedNewtypeDeriving+ , DeriveDataTypeable+ #-} ----------------------------------------------------------------------------- -- |@@ -9,11 +13,11 @@ -- Stability : experimental -- -- Difference strings: a data structure for O(1) append on strings. Note that a--- DString is just a newtype wrapper around a 'DList Char'. The reason we need a--- new type instead of just a type synonym is that we can have an 'instance--- IsString DString' without using language extensions (TypeSynonymInstances or--- FlexibleInstances) so we can write overloaded string literals of type--- DString.+-- 'DString' is just a newtype wrapper around a 'DList' 'Char'. The reason we+-- need a new type instead of just a type synonym is that we can have an+-- @instance 'IsString' 'DString'@ without using language extensions+-- (@TypeSynonymInstances@ or @FlexibleInstances@) so we can write overloaded+-- string literals of type 'DString'. -- ----------------------------------------------------------------------------- @@ -36,118 +40,146 @@ , tail , unfoldr , foldr- )- where+ ) where -import Prelude hiding (concat, foldr, head, tail)-import qualified Data.DList as D-import Data.Monoid (Monoid)-import Data.String (IsString, fromString)-import qualified Data.String.ToString as ToString +--------------------------------------------------------------------------------+-- Imports+-------------------------------------------------------------------------------- +import Prelude ( fromInteger, (>=), error )+import Data.Char ( Char )+import Data.Function ( ($), const, flip )+import Data.List ( map )+import Data.Maybe ( Maybe )+import Data.Monoid ( Monoid )+import Data.Typeable ( Typeable )+import Data.String ( IsString, fromString )+import Text.Show ( Show, showsPrec, ShowS, showParen, showString, shows )++-- from base-unicode-symbols:+import Data.Function.Unicode ( (∘) )++-- from to-string-class:+import Data.String.ToString ( ToString, toString )++-- from dlist:+import Data.DList ( DList(DL), unDL, toList, fromList )+import qualified Data.DList as D ( cons, snoc+ , foldr, unfoldr+ , singleton+ , concat+ )++ -------------------------------------------------------------------------------- -- DString -------------------------------------------------------------------------------- --- | A difference string is a function that given a string, returns--- the original contents of the difference string prepended at the--- given string.+-- | A difference string is a function that given a string, returns the original+-- contents of the difference string prepended at the given string. ----- This structure supports O(1) @append@ en @snoc@ operations on--- strings making it very usefull for append-heavy uses such as--- logging and pretty printing.+-- This structure supports O(1) @mappend@ en @snoc@ operations on strings making+-- it very usefull for append-heavy uses such as logging and pretty printing. ----- You can use it to efficiently show a tree for example:--- (Note that we make use of some functions from the /string-combinators/ package:--- <http://hackage.haskell.org/cgi-bin/hackage-scripts/package/string-combinators>)+-- You can use it to efficiently show a tree for example: (Note that we use some+-- handy functions from the @string-combinators@ package) -- -- > {-# LANGUAGE OverloadedStrings #-} -- >--- > import Data.DString--- > import Data.String.Combinators ((<+>), fromShow, paren)+-- > import Data.DString (toShowS, fromShowS)+-- > import Data.String.Combinators ((<+>), paren) -- > -- > data Tree a = Leaf a | Branch (Tree a) (Tree a) -- > -- > instance Show a => Show (Tree a) where--- > show = toString . go+-- > showsPrec prec = showParen (prec >= funAppPrec) . toShowS . go -- > where--- > go (Leaf x) = "Leaf" <+> fromShow x+-- > go (Leaf x) = "Leaf" <+> fromShowS (showsPrec funAppPrec x) -- > go (Branch l r) = "Branch" <+> paren (go l) <+> paren (go r)-newtype DString = DS { toDList :: D.DList Char -- ^ Convert a difference string to a difference list- } deriving Monoid+-- >+-- > funAppPrec = 10+--+-- Note that a @DString@ can be converted from and to a 'String' using the+-- 'fromString' and 'toString' methods from the 'IsString' and 'ToString'+-- classes respectively.+newtype DString = DS (DList Char) deriving (Monoid, Typeable) instance IsString DString where- fromString = fromDList . D.fromList+ fromString = fromDList ∘ fromList -instance ToString.ToString DString where- toString = D.toList . toDList+instance ToString DString where+ toString = toList ∘ toDList instance Show DString where showsPrec p ds = showParen (p >= 10) $- showString "Data.String.fromString " .- shows (ToString.toString ds)+ showString "Data.String.fromString " ∘+ shows (toString ds) -------------------------------------------------------------------------------- -- Conversions -------------------------------------------------------------------------------- --- | Convert a difference list of Chars to a difference string-fromDList :: D.DList Char -> DString+-- | O(1) Convert a difference list of @Char@s to a difference string.+fromDList ∷ DList Char → DString fromDList = DS --- | Convert a ShowS to a difference string-fromShowS :: ShowS -> DString-fromShowS = fromDList . D.DL+-- | O(1) Convert a difference string to a difference list.+toDList ∷ DString → DList Char+toDList (DS dl) = dl --- | Convert a difference string to a ShowS-toShowS :: DString -> ShowS-toShowS = D.unDL . toDList+-- | O(1) Convert a @ShowS@ to a difference string.+fromShowS ∷ ShowS → DString+fromShowS = fromDList ∘ DL +-- | O(1) Convert a difference string to a @ShowS@.+toShowS ∷ DString → ShowS+toShowS = unDL ∘ toDList + -------------------------------------------------------------------------------- -- Basic functions -------------------------------------------------------------------------------- --- | Build a difference string from a single Char-singleton :: Char -> DString-singleton = fromDList . D.singleton+-- | O(1) Build a difference string from a single @Char@.+singleton ∷ Char → DString+singleton = fromDList ∘ D.singleton --- | /O(1)/, Prepend a Char to a difference string-cons :: Char -> DString -> DString+-- | /O(1)/, Prepend a Char to a difference string.+cons ∷ Char → DString → DString cons c ds = fromDList $ D.cons c (toDList ds) --- | /O(1)/, Append a Char to a difference string-snoc :: DString -> Char -> DString+-- | /O(1)/, Append a @Char@ to a difference string.+snoc ∷ DString → Char → DString snoc ds c = fromDList $ D.snoc (toDList ds) c --- | /O(spine)/, Concatenate difference strings-concat :: [DString] -> DString-concat = fromDList . D.concat . map toDList+-- | /O(spine)/, Concatenate difference strings.+concat ∷ [DString] → DString+concat = fromDList ∘ D.concat ∘ map toDList -- | /O(length ds)/, difference list elimination, head, tail.-list :: b -> (Char -> DString -> b) -> DString -> b+list ∷ α → (Char → DString → α) → DString → α list nill consit ds =- case ToString.toString ds of- [] -> nill- x : xs -> consit x $ fromString xs+ case toString ds of+ [] → nill+ x : xs → consit x $ fromString xs --- | Return the head of the difference string-head :: DString -> Char+-- | Return the head of the difference string.+head ∷ DString → Char head = list (error "Data.DString.head: empty list") const --- | Return the tail of the difference string-tail :: DString -> DString+-- | Return the tail of the difference string.+tail ∷ DString → DString tail = list (error "Data.DString.tail: empty list") (flip const) --- | Unfoldr for difference strings-unfoldr :: (b -> Maybe (Char, b)) -> b -> DString+-- | Unfoldr for difference strings.+unfoldr ∷ (α → Maybe (Char, α)) → α → DString unfoldr pf b = fromDList $ D.unfoldr pf b --- | Foldr over difference strings-foldr :: (Char -> b -> b) -> b -> DString -> b-foldr f b = D.foldr f b . toDList+-- | Foldr over difference strings.+foldr ∷ (Char → α → α) → α → DString → α+foldr f b = D.foldr f b ∘ toDList -- The End ---------------------------------------------------------------------
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2009, Bas van Dijk+Copyright (c) 2009-2010, Bas van Dijk All rights reserved. Redistribution and use in source and binary forms, with or without
dstring.cabal view
@@ -1,18 +1,18 @@ Name: dstring-Version: 0.3.0.1-Synopsis: Difference strings.+Version: 0.3.0.2+Synopsis: Difference strings Description: Difference strings: a data structure for O(1) append on strings. Note that a- DString is just a newtype wrapper around a 'DList Char'. The reason we need a- new type instead of just a type synonym is that we can have an 'instance- IsString DString' without using language extensions (TypeSynonymInstances or- FlexibleInstances) so we can write overloaded string literals of type DString.+ @DString@ is just a newtype wrapper around a @DList Char@. The reason we need a+ new type instead of just a type synonym is that we can have an @instance+ IsString DString@ without using language extensions (@TypeSynonymInstances@ or+ @FlexibleInstances@) so we can write overloaded string literals of type @DString@. Category: Data License: BSD3 License-file: LICENSE Author: Bas van Dijk <v.dijk.bas@gmail.com> Maintainer: Bas van Dijk <v.dijk.bas@gmail.com>-Copyright: 2009 Bas van Dijk <v.dijk.bas@gmail.com>+Copyright: 2009-2010 Bas van Dijk <v.dijk.bas@gmail.com> Cabal-version: >= 1.6 Build-Type: Simple Stability: experimental@@ -23,8 +23,9 @@ Location: http://code.haskell.org/~basvandijk/code/dstring Library- Build-Depends: base == 4.*- , dlist >= 0.5 && < 0.6- , to-string-class >= 0.1.2 && < 0.2+ Build-Depends: base >= 4 && < 4.3+ , base-unicode-symbols >= 0.1.1 && < 0.3+ , dlist >= 0.5 && < 0.6+ , to-string-class >= 0.1.2 && < 0.2 Exposed-modules: Data.DString- Ghc-options: -O2 -Wall+ Ghc-options: -Wall