Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Export named function equivalents of operators

Fixes #24
  • Loading branch information...
commit 3a07917d99991fb004cf26c3aea5b24be20149fb 1 parent e015485
Johan Tibell authored
Showing with 34 additions and 5 deletions.
  1. +5 −0 Data/Csv.hs
  2. +29 −5 Data/Csv/Conversion.hs
5 Data/Csv.hs
View
@@ -51,6 +51,7 @@ module Data.Csv
, FromRecord(..)
, Parser
, runParser
+ , index
, (.!)
, ToRecord(..)
, record
@@ -59,9 +60,11 @@ module Data.Csv
-- ** Name-based record conversion
-- $namebased
, FromNamedRecord(..)
+ , lookup
, (.:)
, ToNamedRecord(..)
, namedRecord
+ , namedField
, (.=)
-- ** Field conversion
@@ -69,6 +72,8 @@ module Data.Csv
, ToField(..)
) where
+import Prelude hiding (lookup)
+
import Data.Csv.Conversion
import Data.Csv.Encoding
import Data.Csv.Types
34 Data/Csv/Conversion.hs
View
@@ -20,8 +20,11 @@ module Data.Csv.Conversion
, runParser
-- * Accessors
+ , index
, (.!)
+ , lookup
, (.:)
+ , namedField
, (.=)
, record
, namedRecord
@@ -48,7 +51,7 @@ import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U
import Data.Word
import GHC.Float (double2Float)
-import Prelude hiding (takeWhile)
+import Prelude hiding (lookup, takeWhile)
import Data.Csv.Conversion.Internal
import Data.Csv.Types
@@ -611,22 +614,40 @@ typeError typ s mmsg =
-- | Retrieve the /n/th field in the given record. The result is
-- 'empty' if the value cannot be converted to the desired type.
-- Raises an exception if the index is out of bounds.
+--
+-- If you're certain that the index is not out of bounds, using
+-- @'parseField' (`V.unsafeIndex` v idx)@ is somewhat faster.
+index :: FromField a => Record -> Int -> Parser a
+index v idx = parseField (v ! idx)
+{-# INLINE index #-}
+
+-- | Alias for 'index'.
(.!) :: FromField a => Record -> Int -> Parser a
-v .! idx = parseField (v ! idx)
+(.!) = index
{-# INLINE (.!) #-}
-- | Retrieve a field in the given record by name. The result is
-- 'empty' if the field is missing or if the value cannot be converted
-- to the desired type.
-(.:) :: FromField a => NamedRecord -> B.ByteString -> Parser a
-m .: name = maybe (fail err) parseField $ HM.lookup name m
+lookup :: FromField a => NamedRecord -> B.ByteString -> Parser a
+lookup m name = maybe (fail err) parseField $ HM.lookup name m
where err = "no field named " ++ show (B8.unpack name)
+{-# INLINE lookup #-}
+
+-- | Alias for 'lookup'.
+(.:) :: FromField a => NamedRecord -> B.ByteString -> Parser a
+(.:) = lookup
{-# INLINE (.:) #-}
-- | Construct a pair from a name and a value. For use with
-- 'namedRecord'.
+namedField :: ToField a => B.ByteString -> a -> (B.ByteString, B.ByteString)
+namedField name val = (name, toField val)
+{-# INLINE namedField #-}
+
+-- | Alias for 'namedField'.
(.=) :: ToField a => B.ByteString -> a -> (B.ByteString, B.ByteString)
-name .= val = (name, toField val)
+(.=) = namedField
{-# INLINE (.=) #-}
-- | Construct a record from a list of 'B.ByteString's. Use 'toField'
@@ -707,6 +728,9 @@ apP d e = do
-- | Run a 'Parser', returning either @'Left' errMsg@ or @'Right'
-- result@. Forces the value in the 'Left' or 'Right' constructors to
-- weak head normal form.
+--
+-- You most likely won't need to use this function directly, but it's
+-- included for completeness.
runParser :: Parser a -> Either String a
runParser p = unParser p left right
where
Please sign in to comment.
Something went wrong with that request. Please try again.