packages feed

string-combinators 0.4 → 0.5

raw patch · 3 files changed

+124/−117 lines, 3 filesdep +base-unicode-symbolsdep ~base

Dependencies added: base-unicode-symbols

Dependency ranges changed: base

Files

Data/String/Combinators.hs view
@@ -1,32 +1,24 @@-{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE OverloadedStrings, NoImplicitPrelude, UnicodeSyntax #-} ------------------------------------------------------------------------------+-------------------------------------------------------------------------------- -- | -- Module      :  Data.String.Combinators--- Copyright   :  (c) 2009 Bas van Dijk+-- Copyright   :  (c) 2009-2010 Bas van Dijk -- License     :  BSD-style (see the file LICENSE)--- -- Maintainer  :  Bas van Dijk <v.dijk.bas@gmail.com>--- Stability   :  Stable--- Portability :  Requires OverloadedStrings ----- Note that I am thinking about putting some of the combinators--- (('<>'), ('<+>'), ('$$') and maybe more) in a type class. This--- allows the \'pretty\' package to use this package.---------------------------------------------------------------------------------+--------------------------------------------------------------------------------  module Data.String.Combinators-    (-      -- * Combining+    ( -- * Combining       (<>)     , mid     , (<+>)     , ($$)     , intercalate     , hcat-    , hsep-    , vcat+    , unwords+    , unlines     , punctuate       -- * Wrapping in delimiters@@ -40,7 +32,6 @@     , quotes     , doubleQuotes -       -- * From characters     , char @@ -67,45 +58,62 @@     , float     , double     , rational+    ) where -    )-    where +--------------------------------------------------------------------------------+-- Imports+-------------------------------------------------------------------------------- -import Data.String (IsString, fromString)-import Data.Monoid (Monoid, mempty, mappend)+-- from base:+import Data.List     ( foldr )+import Data.Bool     ( Bool(False, True) )+import Data.Char     ( Char )+import Data.Function ( id )+import Data.Int      ( Int )+import Data.Ratio    ( Rational )+import Data.String   ( IsString, fromString )+import Data.Monoid   ( Monoid, mempty, mappend )+import Text.Show     ( Show, show )+import Prelude       ( Integer, Float, Double ) +-- from base-unicode-symbols+import Data.Function.Unicode ( (∘) ) -------------------------------------------------------------------------- Combining----------------------------------------------------------------------- +--------------------------------------------------------------------------------+-- * Combining+--------------------------------------------------------------------------------+ -- | Put two string-likes besides eachother. ----- Note that '<>' is just a synonym for 'mappend'.-(<>) :: Monoid s => s -> s -> s+-- Note that: @'<>' = 'mappend'@.+(<>) ∷ Monoid s ⇒ s → s → s (<>) = mappend  -- | @mid m x y@ Puts @x@ and @y@ around @m@. ----- Note that: @mid m x y =@ 'between' @x y m@.-mid :: Monoid s => s -> (s -> s -> s)+-- Note that: @mid m x y = 'between' x y m@.+mid ∷ Monoid s ⇒ s → (s → s → s) mid m x y = between x y m --- | Put two string-likes besides eachother separated by a space.-(<+>) :: (Monoid s, IsString s) => s -> s -> s+-- | Put two string-likes besides eachother separated by a 'space'.+(<+>) ∷ (Monoid s, IsString s) ⇒ s → s → s (<+>) = mid space --- | Put two string-likes above eachother.-($$) :: (Monoid s, IsString s) => s -> s -> s+-- | Put two string-likes above eachother (separated by a 'newline').+($$) ∷ (Monoid s, IsString s) ⇒ s → s → s ($$) = mid newline  infixl 6 <> infixl 6 <+> infixl 5 $$ --- | Combine the string-likes with a given function.-intercalate :: Monoid s => (s -> s -> s) -> [s] -> s+{-| Combine the string-likes with a given function.++@intercalate f [s1, ... sn] = s1 \`f\` (s2 \`f\` (... (sn-1 \`f\` sn)))@+-}+intercalate ∷ Monoid s ⇒ (s → s → s) → [s] → s intercalate f = go     where       go []     = mempty@@ -114,24 +122,26 @@  -- | List version of '<>'. ----- Note that @hcat = 'intercalate' ('<>')@.-hcat :: Monoid s => [s] -> s+-- Note that: @hcat = 'intercalate' ('<>')@.+hcat ∷ Monoid s ⇒ [s] → s hcat = intercalate (<>)  -- | List version of '<+>'. ----- Note that @hsep = 'intercalate' ('<+>')@.-hsep :: (Monoid s, IsString s) => [s] -> s-hsep = intercalate (<+>)+-- Note that: @unwords = 'intercalate' ('<+>')@.+unwords ∷ (Monoid s, IsString s) ⇒ [s] → s+unwords = intercalate (<+>)  -- | List version of '$$'.-vcat :: (Monoid s, IsString s) =>  [s] -> s-vcat = foldr ($$) mempty+--+-- Note that: @unlines = foldr ('$$') mempty@+unlines ∷ (Monoid s, IsString s) ⇒  [s] → s+unlines = foldr ($$) mempty --- | @punctuate p [d1, ... dn] = [d1 '<>' p, d2 '<>' p, ... dn-1 '<>' p, dn]@.+-- | @punctuate p [s1, ... sn] = [s1 '<>' p, s2 '<>' p, ... sn-1 '<>' p, sn]@. ----- Idea and implementation taken from 'pretty':-punctuate :: (Monoid s) => s -> [s] -> [s]+-- (Idea and implementation taken from the @pretty@ package.)+punctuate ∷ (Monoid s) ⇒ s → [s] → [s] punctuate _ []     = [] punctuate p (d:ds) = go d ds     where@@ -139,147 +149,144 @@       go d' (e:es) = (d' <> p) : go e es  -------------------------------------------------------------------------- Wrapping in delimiters-----------------------------------------------------------------------+--------------------------------------------------------------------------------+-- * Wrapping in delimiters+--------------------------------------------------------------------------------  -- | @between b c s@ wraps the string-like @s@ between @b@ and @c@.-between :: (Monoid s) => s -> s -> (s -> s)-between open close x = open <> x <> close+between ∷ (Monoid s) ⇒ s → s → (s → s)+between open close = \x -> open <> x <> close   -- | Wrap a string-like in @(...)@.-paren :: (Monoid s, IsString s) => s -> s+paren ∷ (Monoid s, IsString s) ⇒ s → s paren = between "(" ")"  -- | Wrap a string-like in @[...]@.-brackets :: (Monoid s, IsString s) => s -> s+brackets ∷ (Monoid s, IsString s) ⇒ s → s brackets = between "[" "]"  -- | Wrap a string-like in @{...}@.-braces :: (Monoid s, IsString s) => s -> s+braces ∷ (Monoid s, IsString s) ⇒ s → s braces = between "{" "}"  -- | Wrap a string-like in @\<...\>@.-angleBrackets :: (Monoid s, IsString s) => s -> s+angleBrackets ∷ (Monoid s, IsString s) ⇒ s → s angleBrackets = between "<" ">"  -- | Wrap a string-like in @\'...\'@.-quotes :: (Monoid s, IsString s) => s -> s+quotes ∷ (Monoid s, IsString s) ⇒ s → s quotes = between "'" "'"  -- | Wrap a string-like in @\"...\"@.-doubleQuotes :: (Monoid s, IsString s) => s -> s+doubleQuotes ∷ (Monoid s, IsString s) ⇒ s → s doubleQuotes = between "\"" "\""  -{- | Like 'showParen' conditionally wraps a string in @(...)@+{-| Like @showParen@ conditionally wraps a string-like in @(...)@  This function is supposed to be used infix as in: -@-(precedence >= 10) \`thenParen\` (\"fun\" \<+\> \"arg\")-@+@(precedence >= 10) \`thenParen\` (\"fun\" \<+\> \"arg\")@ -}-thenParen :: (Monoid s, IsString s) => Bool -> s -> s+thenParen ∷ (Monoid s, IsString s) ⇒ Bool → s → s thenParen True  = paren thenParen False = id  -------------------------------------------------------------------------- From characters-----------------------------------------------------------------------+--------------------------------------------------------------------------------+-- * From characters+--------------------------------------------------------------------------------  -- | Convert a character to a string-like.-char :: IsString s => Char -> s+char ∷ IsString s ⇒ Char → s char c = fromString [c]   -- | A ';' character.-semi :: IsString s => s+semi ∷ IsString s ⇒ s semi = char ';'  -- | A ':' character.-colon :: IsString s => s+colon ∷ IsString s ⇒ s colon = char ':'  -- | A ',' character.-comma :: IsString s => s+comma ∷ IsString s ⇒ s comma = char ','  -- | A ' ' character.-space :: IsString s => s+space ∷ IsString s ⇒ s space = char ' '  -- | A '\n' character.-newline :: IsString s => s+newline ∷ IsString s ⇒ s newline = char '\n'  -- | A '=' character.-equals :: IsString s => s+equals ∷ IsString s ⇒ s equals = char '='  -- | A '(' character.-lparen :: IsString s => s+lparen ∷ IsString s ⇒ s lparen = char '('  -- | A ')' character.-rparen :: IsString s => s+rparen ∷ IsString s ⇒ s rparen = char ')'  -- | A '[' character.-lbrack :: IsString s => s+lbrack ∷ IsString s ⇒ s lbrack = char '['  -- | A ']' character.-rbrack :: IsString s => s+rbrack ∷ IsString s ⇒ s rbrack = char ']'  -- | A '{' character.-lbrace :: IsString s => s+lbrace ∷ IsString s ⇒ s lbrace = char '{'  -- | A '}' character.-rbrace :: IsString s => s+rbrace ∷ IsString s ⇒ s rbrace = char '}'  -- | A \'<\' character.-labrack :: IsString s => s+labrack ∷ IsString s ⇒ s labrack = char '<'  -- | A \'>\' character.-rabrack :: IsString s => s+rabrack ∷ IsString s ⇒ s rabrack = char '>'  -------------------------------------------------------------------------- From showable values-----------------------------------------------------------------------+--------------------------------------------------------------------------------+-- * From showable values+-------------------------------------------------------------------------------- --- | Convert a Show-able value to a string.--- @fromShow = fromString . show@-fromShow :: (Show a, IsString s) => a -> s-fromShow = fromString . show+-- | Convert a @Show@able value to a string-like.+fromShow ∷ (Show α, IsString s) ⇒ α → s+fromShow = fromString ∘ show --- | Convert an Int to a string-like.-int :: IsString s => Int -> s+-- | Convert an @Int@ to a string-like.+int ∷ IsString s ⇒ Int → s int = fromShow --- | Convert an Integer to a string-like.-integer :: IsString s => Integer -> s+-- | Convert an @Integer@ to a string-like.+integer ∷ IsString s ⇒ Integer → s integer = fromShow --- | Convert a Float to a string-like.-float :: IsString s => Float -> s+-- | Convert a @Float@ to a string-like.+float ∷ IsString s ⇒ Float → s float = fromShow --- | Convert a Double to a string-like.-double :: IsString s => Double -> s+-- | Convert a @Double@ to a string-like.+double ∷ IsString s ⇒ Double → s double = fromShow --- | Convert a Rational to a string-like.-rational :: IsString s => Rational -> s+-- | Convert a @Rational@ to a string-like.+rational ∷ IsString s ⇒ Rational → s rational = fromShow  --- The End -----------------------------------------------------------+-- 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
string-combinators.cabal view
@@ -1,27 +1,27 @@-Name:                   string-combinators-Version:                0.4-Synopsis:               Polymorphic functions to build and combine stringlike values-Description:            string-combinators provides handy polymorphic functions-                        to build and combine string-like values.-                        .-                        All functions are polymorphic in their string-like type-                        but usually have a Monoid or IsString constraint.-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>-Cabal-version:          >= 1.6-Build-Type:             Simple-Stability:              experimental-Extra-source-files:     LICENSE+Name:          string-combinators+Version:       0.5+Synopsis:      Polymorphic functions to build and combine string-like values+Description:   @string-combinators@ provides handy polymorphic functions+               to build and combine string-like values.+               .+               All functions are polymorphic in their string-like type+               but usually have a 'Monoid' or 'IsString' constraint.+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-2010 Bas van Dijk <v.dijk.bas@gmail.com>+Cabal-version: >= 1.6+Build-Type:    Simple+Stability:     experimental  Source-repository head   Type:     darcs   Location: http://code.haskell.org/~basvandijk/code/string-combinators/  Library-  Build-Depends:        base == 4.*-  Exposed-modules:      Data.String.Combinators-  Ghc-options:          -O2 -Wall+  Build-Depends: base                 >= 4     && < 4.3+               , base-unicode-symbols >= 0.1.1 && < 0.3+  Exposed-modules: Data.String.Combinators+  Ghc-options: -Wall