Commit b13df0ac authored by Mike Ledger's avatar Mike Ledger

cache results as they are built up

parent ab34e1ea
......@@ -37,4 +37,5 @@ executable csvmaps
, parsers
, optparse-applicative
, lens
, transformers
default-language: Haskell2010
......@@ -27,6 +27,7 @@ dependencies:
- parsers
- optparse-applicative
- lens
- transformers
executables:
csvmaps:
......
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Control.Lens (failing, re, to, (^?), _Left,
_Right)
import Control.Monad.Trans.Resource (MonadResource, runResourceT)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Streaming as B
import qualified Data.ByteString.Streaming.Char8 as Q (lines)
import Data.Csv (HasHeader (..))
import Data.List (isSuffixOf)
import qualified Data.Map.Strict as Map
import Control.Lens (failing, re, to, (^?), _Left,
_Right)
import Control.Monad.Trans.Resource (MonadResource, runResourceT)
import Control.Monad.Trans.State.Strict
import Data.ByteString (ByteString)
import qualified Data.ByteString.Streaming as B
import qualified Data.ByteString.Streaming.Char8 as Q (lines)
import Data.Csv (HasHeader (..))
import Data.List (isSuffixOf)
import qualified Data.Map.Strict as Map
import Data.Typeable
import qualified Data.Vector as V
import GHC.Generics (Generic)
import qualified Data.Vector as V
import GHC.Generics (Generic)
import Options.Generic
import Streaming
import qualified Streaming.Csv as Csv
import qualified Streaming.Prelude as S
import Data.Char (toUpper)
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import qualified Options.Applicative as Options
import Text.Parser.Expression (Assoc (..), Operator (..),
buildExpressionParser)
import Text.Trifecta (Parser)
import qualified Text.Trifecta as P
import qualified Streaming.Csv as Csv
import qualified Streaming.Prelude as S
import Data.Char (toUpper)
import Data.Maybe (fromMaybe)
import qualified Data.Text as T
import qualified Options.Applicative as Options
import Text.Parser.Expression (Assoc (..), Operator (..),
buildExpressionParser)
import Text.Trifecta (Parser)
import qualified Text.Trifecta as P
data MapOp
= OpIntersection
......@@ -152,16 +153,25 @@ output opts = case unHelpful (outfile opts) of
Just out -> B.writeFile out
Nothing -> B.stdout
interpret :: (Monoid a, Ord k) => MapExpr -> V.Vector (Map.Map k a) -> Map.Map k a
interpret me0 v = go me0
interpret :: forall a k. (Monoid a, Ord k) => MapExpr -> V.Vector (Map.Map k a) -> Map.Map k a
interpret me0 v = evalState (go me0) Map.empty
where
go me = case me of
DocNum i -> v V.! (i - 1)
Union a b -> Map.union (go a) (go b)
Difference a b -> Map.difference (go a) (go b)
Intersection a b -> Map.intersection (go a) (go b)
LongUnion a b -> Map.unionWith mappend (go a) (go b)
LongIntersection a b -> Map.intersectionWith mappend (go a) (go b)
go :: MapExpr
-> State (Map.Map MapExpr (Map.Map k a)) (Map.Map k a)
go me = do
cache <- get
case Map.lookup me cache of
Just x -> return x
Nothing -> do
r <- case me of
DocNum i -> return (v V.! (i - 1))
Union a b -> Map.union <$> go a <*> go b
Difference a b -> Map.difference <$> go a <*> go b
Intersection a b -> Map.intersection <$> go a <*> go b
LongUnion a b -> Map.unionWith mappend <$> go a <*> go b
LongIntersection a b -> Map.intersectionWith mappend <$> go a <*> go b
modify' (Map.insert me r)
return r
-- | select the combining algorithm
combine :: (Monoid a, Ord k) => Options -> [Map.Map k a] -> Map.Map k a
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment