Permalink
Browse files

Get rid of map newtypes

  • Loading branch information...
1 parent 6c85fbd commit 0ed5f8760702e2bf1767d50968eb59d342a7c422 @tibbe committed Aug 24, 2012
Showing with 12 additions and 31 deletions.
  1. +0 −2 Data/Ceason.hs
  2. +8 −29 Data/Ceason/Conversion.hs
  3. +3 −0 benchmarks/Benchmarks.hs
  4. +1 −0 ceason.cabal
View
@@ -59,8 +59,6 @@ module Data.Ceason
, ToNamedRecord(..)
, namedRecord
, (.=)
- , BSMap(..)
- , BSHashMap(..)
-- ** Field conversion
, FromField(..)
View
@@ -4,8 +4,6 @@ module Data.Ceason.Conversion
-- * Type conversion
Only(..)
, FromRecord(..)
- , BSMap(..)
- , BSHashMap(..)
, FromNamedRecord(..)
, ToNamedRecord(..)
, FromField(..)
@@ -243,24 +241,6 @@ instance ToField a => ToRecord (Vector a) where
------------------------------------------------------------------------
-- Name-based conversion
--- | A 'M.Map' keyed by 'B.ByteString' keys.
---
--- The primary use case of 'BSMap' is to decode a CSV file into a
--- @'BSMap' 'B.ByteString'@, which lets you process the CSV data
--- without converting it to a more specific type.
-newtype BSMap a = BSMap {
- fromBSMap :: M.Map B.ByteString a
- } deriving (Eq, Ord, Read, Show)
-
--- | A 'HM.HashMap' keyed by 'B.ByteString' keys.
---
--- The primary use case of 'BSHashMap' is to decode a CSV file into a
--- @'BSHashMap' 'B.ByteString'@, which lets you process the CSV data
--- without converting it to a more specific type.
-newtype BSHashMap a = BSHashMap {
- fromBSHashMap :: HM.HashMap B.ByteString a
- } deriving (Eq, Show)
-
-- | A type that can be converted from a single CSV record, with the
-- possibility of failure.
--
@@ -304,20 +284,19 @@ class FromNamedRecord a where
class ToNamedRecord a where
toNamedRecord :: a -> NamedRecord
-instance FromField a => FromNamedRecord (BSMap a) where
- parseNamedRecord m = BSMap . M.fromList <$>
+instance FromField a => FromNamedRecord (M.Map B.ByteString a) where
+ parseNamedRecord m = M.fromList <$>
(traverse parseSnd $ HM.toList m)
where parseSnd (name, s) = (,) <$> pure name <*> parseField s
-instance ToField a => ToNamedRecord (BSMap a) where
- toNamedRecord = HM.fromList . map (\ (k, v) -> (k, toField v)) . M.toList .
- fromBSMap
+instance ToField a => ToNamedRecord (M.Map B.ByteString a) where
+ toNamedRecord = HM.fromList . map (\ (k, v) -> (k, toField v)) . M.toList
-instance FromField a => FromNamedRecord (BSHashMap a) where
- parseNamedRecord m = BSHashMap <$> traverse (\ s -> parseField s) m
+instance FromField a => FromNamedRecord (HM.HashMap B.ByteString a) where
+ parseNamedRecord m = traverse (\ s -> parseField s) m
-instance ToField a => ToNamedRecord (BSHashMap a) where
- toNamedRecord = HM.map toField . fromBSHashMap
+instance ToField a => ToNamedRecord (HM.HashMap B.ByteString a) where
+ toNamedRecord = HM.map toField
------------------------------------------------------------------------
-- Individual field conversion
View
@@ -7,6 +7,7 @@ import Criterion.Main
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
+import qualified Data.HashMap.Strict as HM
import Data.Text (Text)
import Data.Vector (Vector)
@@ -39,6 +40,8 @@ instance ToNamedRecord President where
fromStrict s = BL.fromChunks [s]
+type BSHashMap a = HM.HashMap B.ByteString a
+
main :: IO ()
main = do
!csvData <- fromStrict `fmap` B.readFile "benchmarks/presidents.csv"
View
@@ -65,5 +65,6 @@ Benchmark benchmarks
ceason,
criterion,
text,
+ unordered-containers,
vector
hs-source-dirs: benchmarks

0 comments on commit 0ed5f87

Please sign in to comment.