packages feed

ListWriter (empty) → 0.1.0.0

raw patch · 6 files changed

+142/−0 lines, 6 filesdep +ListWriterdep +basedep +hspecsetup-changed

Dependencies added: ListWriter, base, hspec, mtl

Files

+ ListWriter.cabal view
@@ -0,0 +1,35 @@+name:                ListWriter+version:             0.1.0.0+synopsis:            define a list constant using Monadic syntax other than overhead [,]+homepage:            https://github.com/YLiLarry/ListWriter+license:             BSD3+author:              Yu Li+maintainer:          ylilarry@gmail.com+copyright:           2010 Yu Li+category:            Syntax+build-type:          Simple+-- extra-source-files:+cabal-version:       >=1.10++library+  hs-source-dirs:      src+  exposed-modules:+                       Syntax.ListWriter+                     , Syntax.ListWriter.Internal++  build-depends:       base >= 4.7 && < 5+                     , mtl++  default-language:    Haskell2010++test-suite ListWriter-test+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             Spec.hs+  other-modules:+                       Test.ListWriter+  build-depends:       base+                     , ListWriter+                     , hspec+  ghc-options:         -threaded -rtsopts -with-rtsopts=-N+  default-language:    Haskell2010
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Syntax/ListWriter.hs view
@@ -0,0 +1,40 @@+{-# LANGUAGE Safe #-}+-----------------------------------------------------------------------------+-- |+-- Module      :  Syntax.ListWriter+-- Copyright   :  BSD+--+-- Maintainer  :  ylilarry@gmail.com+-- Stability   :  Experimental+-- Portability :  Non-portable (GHC extensions)+--+-- This package allows you to define a list constant using Monadic syntax.+--+-- It can be used to define a list constant in your program in a better looking syntax than overhead @[@, @]@, and @,@.+--+-- Other than that the package is not really useful.+--+-- === Example+--+-- @+-- listA :: [Int]+-- listA = toList $ do+--    element 1+--    element 2+--    element 3+-- @+--+-- >>> print listA+-- [1,2,3]+--+-----------------------------------------------------------------------------++module Syntax.ListWriter (+        ListM+      , fromList+      , toList+      , element+    ) where++import Syntax.ListWriter.Internal+
+ src/Syntax/ListWriter/Internal.hs view
@@ -0,0 +1,33 @@+{-# LANGUAGE Trustworthy #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE DeriveFunctor #-}++module Syntax.ListWriter.Internal where++import Control.Monad.Writer (Writer(..), runWriter, tell, MonadWriter(..))++newtype ListM' a x = ListM {+      unListM :: Writer [a] x+   } deriving (Functor, Applicative, Monad, MonadWriter [a], Show, Ord, Eq)++type ListM a = ListM' a ()+++toList :: ListM a -> [a]+toList m = snd $ runWriter $ unListM m+++fromList :: [a] -> ListM a+fromList = tell+++element :: a -> ListM a+element a = fromList [a]+++-- addElems :: [a] -> ListM a -> ListM a+-- addElems ls m = m >> tell ls+++-- empty :: ListM a+-- empty = ListM $ tell []
+ test/Spec.hs view
@@ -0,0 +1,8 @@+import Test.Hspec++import Test.ListWriter++main :: IO ()+main = do+   Test.ListWriter.test+
+ test/Test/ListWriter.hs view
@@ -0,0 +1,24 @@+module Test.ListWriter where++import Syntax.ListWriter+import Test.Hspec++listA :: ListM Int+listA = do+   element 1+   element 2++listE :: ListM Int+listE = fromList []++test :: IO ()+test = hspec $+   describe "ListWriter" $ do+      specify "toList" $ do+         toList listA `shouldBe` [1,2]+         toList listE `shouldBe` []+      specify "fromList" $ do+         fromList [1,2] `shouldBe` listA+         fromList [] `shouldBe` listE++