packages feed

formatting 6.1.2 → 6.2.0

raw patch · 9 files changed

+90/−102 lines, 9 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Formatting.Holey: (%) :: Format b c -> Format r b -> Format r c
- Formatting.Holey: (%.) :: Format r (Builder -> b) -> Format b c -> Format r c
- Formatting.Holey: Holey :: ((m -> r) -> a) -> HoleyT r a m
- Formatting.Holey: bind :: Format b c -> (Builder -> Format a b) -> Format a c
- Formatting.Holey: instance Functor (HoleyT r a)
- Formatting.Holey: instance Monoid (Format r (a -> r))
- Formatting.Holey: instance a ~ r => IsString (Format r a)
- Formatting.Holey: later :: (a -> Builder) -> Format r (a -> r)
- Formatting.Holey: newtype HoleyT r a m
- Formatting.Holey: now :: Builder -> Format r r
- Formatting.Holey: runHM :: HoleyT r a m -> (m -> r) -> a
- Formatting.Holey: type Format r x = Holey Builder r x
- Formatting.Holey: type Holey m r a = HoleyT r a m
+ Formatting: (%) :: Format r a -> Format r' r -> Format r' a
+ Formatting: (%.) :: Format r (Builder -> r') -> Format r' a -> Format r a
+ Formatting: data Format r a
+ Formatting: later :: (a -> Builder) -> Format r (a -> r)
+ Formatting: now :: Builder -> Format r r
+ Formatting.Internal: (%) :: Format r a -> Format r' r -> Format r' a
+ Formatting.Internal: (%.) :: Format r (Builder -> r') -> Format r' a -> Format r a
+ Formatting.Internal: Format :: ((Builder -> r) -> a) -> Format r a
+ Formatting.Internal: bind :: Format r a -> (Builder -> Format r' r) -> Format r' a
+ Formatting.Internal: instance Category Format
+ Formatting.Internal: instance Monoid (Format r (a -> r))
+ Formatting.Internal: instance a ~ r => IsString (Format r a)
+ Formatting.Internal: later :: (a -> Builder) -> Format r (a -> r)
+ Formatting.Internal: newtype Format r a
+ Formatting.Internal: now :: Builder -> Format r r
+ Formatting.Internal: runFormat :: Format r a -> (Builder -> r) -> a

Files

CHANGELOG.md view
@@ -1,3 +1,10 @@+6.2.0++* Dropped Holey/HoleyT in favour of simpler Format type.+* Added Monoid instance.+* Added back Category instance.+* Dropped Functor instance.+ 6.1.1  * Add support for GHC 7.10 (time update).
formatting.cabal view
@@ -1,5 +1,5 @@ name:                formatting-version:             6.1.2+version:             6.2.0 synopsis:            Combinator-based type-safe formatting (like printf() or FORMAT) description:         Combinator-based type-safe formatting (like printf() or FORMAT), modelled from the HoleyMonoids package. license:             BSD3@@ -14,7 +14,6 @@  library   exposed-modules:   Formatting,-                     Formatting.Holey,                      Formatting.Formatters,                      Formatting.ShortFormatters,                      Formatting.Examples,
src/Formatting.hs view
@@ -21,6 +21,11 @@  module Formatting   (+  Format,+  (%),+  (%.),+  now,+  later,   -- * Top-level functions   format,   sformat,@@ -28,12 +33,10 @@   fprint,   hprint,   -- * Formatting library-  module Formatting.Holey,   module Formatting.Formatters,   -- * Other functions   formatToString  ) where  import Formatting.Formatters-import Formatting.Holey import Formatting.Internal
src/Formatting/Clock.hs view
@@ -7,6 +7,7 @@ module Formatting.Clock where  import Formatting+import Formatting.Internal import System.Clock  -- | Format the duration from start to end (args passed in that order).@@ -20,7 +21,7 @@ -- 19.38 µs -- @ timeSpecs :: Format r (TimeSpec -> TimeSpec -> r)-timeSpecs = Holey (\g x y -> g (fmt x y))+timeSpecs = Format (\g x y -> g (fmt x y))   where fmt (TimeSpec s1 n1) (TimeSpec s2 n2)           | Just i <- scale ((10 ^ 9) * 60 * 60 * 24) = bprint (fixed 2 % " d") i           | Just i <- scale ((10 ^ 9) * 60 * 60) = bprint (fixed 2 % " h") i
src/Formatting/Formatters.hs view
@@ -56,7 +56,7 @@   Buildable   ) where -import           Formatting.Holey+import           Formatting.Internal  import           Data.Char (chr, ord) import           Numeric (showIntAtBase)
− src/Formatting/Holey.hs
@@ -1,84 +0,0 @@-{-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE TypeSynonymInstances #-}-{-# LANGUAGE RankNTypes #-}-{-# LANGUAGE GADTs #-}-{-# LANGUAGE OverloadedStrings #-}-{-# OPTIONS -Wall #-}---- |--- Module      : Formatting.Holey--- Copyright   : (c) 2013 Chris Done, 2013 Shachaf Ben-Kiki--- License     : BSD3--- Maintainer  : chrisdone@gmail.com--- Stability   : experimental--- Portability : GHC------ Copy of the holey monoids library but with constructor exported.--module Formatting.Holey-  (-  -- * Formatting library-  Format,-  Holey,-  HoleyT (..),-  (%),-  (%.),-  now,-  bind,-  later-  ) where--import Data.Monoid-import Data.String-import Data.Text.Lazy.Builder (Builder)---- | A formatter.-type Format r x = Holey Builder r x---- | The type of a monoid with holes. The underlying monoid is--- represented by type parameter @m@. The @r@ is the result type and--- stays polymorphic until the very last moment when 'run' is--- called. The last argument @a@ is always a function with zero or--- more arguments, finally resulting in @r@. Ordering the arguments in--- this order allows holey monoids to be composed using `.`, stacking--- the expected arguments. Note that the `Monoid` constraint is only--- used in the identity 'Holey' and in composing two 'Holey's.-newtype HoleyT r a m = Holey { runHM :: (m -> r) -> a }--type Holey m r a = HoleyT r a m--instance Monoid (Format r (a -> r)) where-  mappend m n = Holey (\k a -> runHM m (\b1 -> runHM n (\b2 -> k (b1 <> b2)) a) a)-  mempty = Holey (\k a -> k mempty)--instance Functor (HoleyT r a) where-  fmap g m = Holey (\k -> runHM m (k . g))---- | Very useful instance for writing format string.-instance (a ~ r) => IsString (Format r a) where-  fromString = now . fromString---- | Composition operator. The same as category composition.-(%) :: Format b c -> Format r b -> Format r c-f % g = f `bind` \a -> g `bind` \b -> now (a `mappend` b)-infixr 9 %---- | Function compose two holeys. Will feed the result of one holey--- into another.-(%.) :: Format r (Builder -> b) -> Format b c -> Format r c-(%.) (Holey a) (Holey b) = Holey (b . a)-infixr 8 %.---- | Insert a constant monoidal value.-now :: Builder -> Format r r-now a = Holey ($ a)---- | Monadic indexed bind for holey monoids.-bind :: Format b c -> (Builder -> Format a b) -> Format a c-m `bind` f = Holey $ \k -> runHM m (\a -> runHM (f a) k)---- | Insert a monoidal value that is not specified until the--- computation is 'run'. The argument that is expected later is--- converted to the monoid type using the given conversion function.-later :: (a -> Builder) -> Format r (a -> r)-later f = Holey (. f)
src/Formatting/Internal.hs view
@@ -1,39 +1,102 @@+{-# LANGUAGE GADTs #-}+{-# LANGUAGE FlexibleInstances #-}+ -- | Internal format starters.  module Formatting.Internal where -import           Formatting.Holey--import qualified Data.Text.Lazy as TL-import qualified Data.Text.Lazy.Builder as TLB+import           Control.Category (Category(..))+import           Data.Monoid+import           Data.String import qualified Data.Text as S (Text) import           Data.Text.Lazy (Text) import qualified Data.Text.Lazy as T+import qualified Data.Text.Lazy as TL import           Data.Text.Lazy.Builder (Builder) import qualified Data.Text.Lazy.Builder as T+import qualified Data.Text.Lazy.Builder as TLB import qualified Data.Text.Lazy.IO as T+import           Prelude hiding ((.),id) import           System.IO +-- | A formatter. The @r@ type means the returned value at the+-- end. The more formatters you compose, the more this wil build up+-- arguments from @r@ to @Int -> r@ to @Char -> (Int -> r)@, etc.+newtype Format r a =+  Format {runFormat :: (Builder -> r) -> a}++-- | Useful instance for applying two formatters to the same input+-- argument. For example: @format (year <> "/" % month) now@ will+-- yield @"2015/01"@.+instance Monoid (Format r (a -> r)) where+  mappend m n =+    Format (\k a ->+              runFormat m (\b1 -> runFormat n (\b2 -> k (b1 <> b2)) a) a)+  mempty = Format (\k _ -> k mempty)++-- | Useful instance for writing format string. With this you can+-- write @"Foo"@ instead of @now "Foo!"@.+instance (a ~ r) => IsString (Format r a) where+  fromString = now . fromString++-- | The same as (%). At present using 'Category' has an import+-- overhead, but one day it might be imported as standard.+instance Category Format where+  id = now mempty+  f . g =+    f `bind`+    \a ->+      g `bind`+      \b -> now (a `mappend` b)++-- | Composition operator. 'Format' is an instance of 'Category', but+-- that is (at present) inconvenient to use with regular "Prelude". So+-- this function is provided as a convenience.+(%) :: Format r a -> Format r' r -> Format r' a+(%) = (.)+infixr 9 %++-- | Function compose two formatters. Will feed the result of one+-- formatter into another.+(%.) :: Format r (Builder -> r') -> Format r' a -> Format r a+(%.) (Format a) (Format b) = Format (b . a)+infixr 8 %.++-- | Insert a constant monoidal value.+now :: Builder -> Format r r+now a = Format ($ a)++-- | Monadic indexed bind for holey monoids.+bind :: Format r a -> (Builder -> Format r' r) -> Format r' a+m `bind` f = Format $ \k -> runFormat m (\a -> runFormat (f a) k)++-- | Insert a function which accepts some argument and produces a+-- 'Builder' which is appended to the output at the end.+--+-- @later (f :: Int -> Builder)@ produces @Format r (Int -> r)@.+later :: (a -> Builder) -> Format r (a -> r)+later f = Format (. f)+ -- | Run the formatter and return a lazy 'Text' value. format :: Format Text a -> a-format m = runHM m T.toLazyText+format m = runFormat m T.toLazyText  -- | Run the formatter and return a strict 'S.Text' value. sformat :: Format S.Text a -> a-sformat m = runHM m (T.toStrict . T.toLazyText)+sformat m = runFormat m (T.toStrict . T.toLazyText)  -- | Run the formatter and return a 'Builder' value. bprint :: Format Builder a -> a-bprint m = runHM m id+bprint m = runFormat m id  -- | Run the formatter and print out the text to stdout. fprint :: Format (IO ()) a -> a-fprint m = runHM m (T.putStr . T.toLazyText)+fprint m = runFormat m (T.putStr . T.toLazyText)  -- | Run the formatter and put the output onto the given 'Handle'. hprint :: Handle -> Format (IO ()) a -> a-hprint h m = runHM m (T.hPutStr h . T.toLazyText)+hprint h m = runFormat m (T.hPutStr h . T.toLazyText)  -- | Run the formatter and return a list of characters. formatToString :: Format [Char] a -> a-formatToString m = runHM m (TL.unpack . TLB.toLazyText)+formatToString m = runFormat m (TL.unpack . TLB.toLazyText)
src/Formatting/ShortFormatters.hs view
@@ -16,7 +16,7 @@ module Formatting.ShortFormatters where  import           Formatting.Formatters (bin, int, oct)-import           Formatting.Holey+import           Formatting.Internal  import qualified Data.Text.Buildable as B (build) import qualified Data.Text as S
src/Formatting/Time.hs view
@@ -9,7 +9,6 @@ import           Data.List import           Data.Text.Lazy.Builder import           Formatting.Formatters  hiding (build)-import           Formatting.Holey import           Formatting.Internal  import           Data.Text              (Text)