<commit_before>module Forex where

import Text.ParserCombinators.Parsec

import Control.Monad (liftM2)

import Data.Time.Clock
import Data.Time.Format (parseTime)
import Data.Maybe

import System.Locale (defaultTimeLocale)

data Currency = AUD
              | CAD
              | CHF
              | JPY
              | USD
              | EUR
              | DKK
              | GBP
              | NOK
              | NZD
              | SEK
                deriving (Read,Show)

type CurrencyPair = (Currency,Currency)

data ForexEntry = ForexEntry {
      lTid :: Integer
    , currencyPair :: CurrencyPair
    , rateDateTime :: UTCTime
    , rateBid :: Double
    , rateAsk :: Double
    } deriving Show

forexHistory :: GenParser Char st [ForexEntry]
forexHistory = header >> eol >> many entry

header :: GenParser Char st [String]
header = sepBy name (char ',')

eol :: GenParser Char st Char
eol = char '\n'

name :: GenParser Char st String
name = many (noneOf ",\n")

currencyPairParse :: GenParser Char st CurrencyPair
currencyPairParse = liftM2 (,) currencyParse (char '/' >> currencyParse)

currencyParse :: GenParser Char st Currency
currencyParse = do
  s <- many (noneOf "/,\n")
  return (read s)

-- 1029209135,D,AUD/CAD,2010-01-03 17:03:04,.944900,.945800
entry :: GenParser Char st ForexEntry
entry = do
  ticker <- parseInteger
  _ <- string ",D,"
  trade <- currencyPairParse
  _ <- char ','
  time <- timeParser
  _ <- char ','
  bid <- parseDouble
  _ <- char ','
  ask <- parseDouble
  _ <- eol
  return (ForexEntry ticker trade time bid ask)

parseInteger :: GenParser Char st Integer
parseInteger = do
  s <- many (noneOf ",\n")
  return (read s)

parseDouble :: GenParser Char st Double
parseDouble = do
  s <- many (noneOf ",\n")
  return (readDouble s) 

readDouble :: String -> Double<commit_msg>Lift up reading an entry.  Again, considerable simplifies code.<commit_after>module Forex where

import Text.ParserCombinators.Parsec

import Control.Monad (liftM2,liftM5)

import Data.Time.Clock
import Data.Time.Format (parseTime)
import Data.Maybe

import System.Locale (defaultTimeLocale)

data Currency = AUD
              | CAD
              | CHF
              | JPY
              | USD
              | EUR
              | DKK
              | GBP
              | NOK
              | NZD
              | SEK
                deriving (Read,Show)

type CurrencyPair = (Currency,Currency)

data ForexEntry = ForexEntry {
      lTid :: Integer
    , currencyPair :: CurrencyPair
    , rateDateTime :: UTCTime
    , rateBid :: Double
    , rateAsk :: Double
    } deriving Show

forexHistory :: GenParser Char st [ForexEntry]
forexHistory = header >> eol >> endBy entry eol

header :: GenParser Char st [String]
header = sepBy name (char ',')

eol :: GenParser Char st Char
eol = char '\n'

name :: GenParser Char st String
name = many (noneOf ",\n")

currencyPairParse :: GenParser Char st CurrencyPair
currencyPairParse = liftM2 (,) currencyParse (char '/' >> currencyParse)

currencyParse :: GenParser Char st Currency
currencyParse = do
  s <- many (noneOf "/,\n")
  return (read s)

-- 1029209135,D,AUD/CAD,2010-01-03 17:03:04,.944900,.945800
entry :: GenParser Char st ForexEntry
entry = liftM5 ForexEntry parseInteger
                          (string ",D," >> currencyPairParse)
                          (char ',' >> timeParser)
                          (char ',' >> parseDouble)
                          (char ',' >> parseDouble)

parseInteger :: GenParser Char st Integer
parseInteger = do
  s <- many (noneOf ",\n")
  return (read s)

parseDouble :: GenParser Char st Double
parseDouble = do
  s <- many (noneOf ",\n")
  return (readDouble s) 

readDouble :: String -> Double
{'fraction_non_alphanumeric': 0.10497981157469717, 'fraction_numerical': 0.020995962314939436, 'mean_word_length': 2.396709323583181, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '66029', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1118, 'n_words': 344}