Commit 8217b1ff authored by Mike Ledger's avatar Mike Ledger

new functions

1. filter by non-null rows
2. filter by null rows
3. extract keys only
parent 6c10bbbc
......@@ -55,6 +55,9 @@ data MapExpr k a
| IntersectionWithAll (MapExpr k a) (MapExpr k a)
| IntersectionWithChooseLeft (MapExpr k a) (MapExpr k a)
| IntersectionWithChooseRight (MapExpr k a) (MapExpr k a)
| NonNullsOf (MapExpr k a)
| NullsOf (MapExpr k a)
| Keys (MapExpr k a)
| DocNum {-# UNPACK #-} !Int
| Labelled {-# UNPACK #-} !Text (MapExpr k a)
| Label !Text
......@@ -73,7 +76,11 @@ chooseL _ a = a
parseMapExpr :: Parser (MapExpr k a)
parseMapExpr =
buildExpressionParser
[ [ binary "|*|" IntersectionWithAll AssocLeft
[ [ prefix "nulls" NullsOf
, prefix "non-nulls" NonNullsOf
, prefix "keys" Keys
]
, [ binary "|*|" IntersectionWithAll AssocLeft
, binary "|*" IntersectionWithChooseLeft AssocLeft
, binary "*|" IntersectionWithChooseRight AssocLeft
, binary "*" Intersection AssocLeft
......@@ -96,8 +103,8 @@ parseMapExpr =
Label <$> P.stringLiteral)
<|> P.parens parseMapExpr
binary name fun =
Infix (fun <$ P.textSymbol name)
prefix name fun = Prefix (fun <$ P.textSymbol name)
binary name fun = Infix (fun <$ P.textSymbol name)
instance (Typeable k, Typeable a) => ParseField (MapExpr k a) where
parseField h m c = case m of
......@@ -211,7 +218,11 @@ interpret me0 v = case runState (go me0) Map.empty of
withResult2 f (Result a) (Result b) = Result (f a b)
withResult2 _ _ _ = Error
ins k = k <$ modify' (Map.insert k k)
withResult1 f (Result a) = Result (f a)
withResult1 _ _ = Error
ins k =
k <$ modify' (Map.insert k k)
go :: MapExpr k a
-> State (Map.Map (MapExpr k a) (MapExpr k a)) (MapExpr k a)
......@@ -235,6 +246,9 @@ interpret me0 v = case runState (go me0) Map.empty of
IntersectionWithAll a b -> ins =<< withResult2 (Map.intersectionWith mappend) <$> go a <*> go b
IntersectionWithChooseLeft a b -> ins =<< withResult2 (Map.intersectionWith chooseL) <$> go a <*> go b
IntersectionWithChooseRight a b -> ins =<< withResult2 (Map.intersectionWith chooseR) <$> go a <*> go b
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
_ -> return me
-- | select the combining algorithm
......
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