Commit 1a742e9d authored by Mike Ledger's avatar Mike Ledger

support padding and extraction of columns

parent 8217b1ff
......@@ -22,6 +22,7 @@ import Data.List (isSuffixOf)
import qualified Data.Map.Strict as Map
import Data.Typeable
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import GHC.Generics (Generic)
import Options.Generic
import Streaming
......@@ -58,6 +59,8 @@ data MapExpr k a
| NonNullsOf (MapExpr k a)
| NullsOf (MapExpr k a)
| Keys (MapExpr k a)
| Pad {-# UNPACK #-} !Int (MapExpr k a)
| Columns {-# UNPACK #-} !(VU.Vector Int) (MapExpr k a)
| DocNum {-# UNPACK #-} !Int
| Labelled {-# UNPACK #-} !Text (MapExpr k a)
| Label !Text
......@@ -79,6 +82,17 @@ parseMapExpr =
[ [ prefix "nulls" NullsOf
, prefix "non-nulls" NonNullsOf
, prefix "keys" Keys
, Prefix $ do
_ <- P.textSymbol "pad"
amount <- P.natural
pure (Pad (fromInteger amount))
, Prefix $ do
_ <- P.textSymbol "col"
cols <- (:[]) <$> P.natural <|> P.between
(P.textSymbol "[")
(P.textSymbol "]")
(P.sepBy1 P.natural (P.textSymbol ","))
pure (Columns (VU.fromList (map fromInteger cols)))
]
, [ binary "|*|" IntersectionWithAll AssocLeft
, binary "|*" IntersectionWithChooseLeft AssocLeft
......@@ -196,7 +210,7 @@ output opts = case unHelpful (outfile opts) of
Nothing -> B.stdout
interpret
:: forall a k e. (Monoid a, Ord k, Ord a, a ~ [e])
:: forall a k e. (Monoid a, Monoid e, Ord k, Ord a, a ~ [e])
=> MapExpr k a
-> V.Vector (Map.Map k a)
-> (Map.Map Text (Map.Map k a), Map.Map k a)
......@@ -224,6 +238,16 @@ interpret me0 v = case runState (go me0) Map.empty of
ins k =
k <$ modify' (Map.insert k k)
pad i l
| i > 0 = case l of
x:xs -> x : pad (i - 1) xs
[] -> replicate i mempty
| otherwise = l
cols cs l = VU.foldr
(\i xs -> fromMaybe mempty (lv V.!? i) : xs) [] cs
where lv = V.fromList l
go :: MapExpr k a
-> State (Map.Map (MapExpr k a) (MapExpr k a)) (MapExpr k a)
go me = do
......@@ -249,10 +273,12 @@ interpret me0 v = case runState (go me0) Map.empty of
NonNullsOf a -> ins =<< withResult1 (Map.filter (not . null)) <$> go a
NullsOf a -> ins =<< withResult1 (Map.filter null) <$> go a
Keys a -> ins =<< withResult1 (Map.map (const [])) <$> go a
Pad i a -> ins =<< withResult1 (Map.map (pad i)) <$> go a
Columns cs a -> ins =<< withResult1 (Map.map (cols cs)) <$> go a
_ -> return me
-- | select the combining algorithm
combine :: (Monoid a, Ord k, Ord a, a ~ [e])
combine :: (Monoid a, Monoid e, Ord k, Ord a, a ~ [e])
=> Options k a
-> [Map.Map k a]
-> (Map.Map Text (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