Query with regexes
This commit is contained in:
		@@ -8,10 +8,12 @@ module Data.Query
 | 
			
		||||
    -- * Combinators
 | 
			
		||||
  , startsWith
 | 
			
		||||
  , endsWith
 | 
			
		||||
  , regex
 | 
			
		||||
  , (.&&.)
 | 
			
		||||
 | 
			
		||||
    -- * Evaluating queries
 | 
			
		||||
  , evaluate
 | 
			
		||||
  , predicate
 | 
			
		||||
  )
 | 
			
		||||
  where
 | 
			
		||||
 | 
			
		||||
@@ -25,7 +27,7 @@ import Text.Regex.TDFA
 | 
			
		||||
       ((=~))
 | 
			
		||||
 | 
			
		||||
import Data.Functor.Foldable
 | 
			
		||||
       (Fix(..))
 | 
			
		||||
       (Fix(..), cata)
 | 
			
		||||
 | 
			
		||||
data Field a where
 | 
			
		||||
  Url :: Field String
 | 
			
		||||
@@ -47,6 +49,9 @@ startsWith field x = Fix (StartsWith field x)
 | 
			
		||||
endsWith :: Field String -> String -> Query
 | 
			
		||||
endsWith field x = Fix (EndsWith field x)
 | 
			
		||||
 | 
			
		||||
regex :: Field String -> String -> Query
 | 
			
		||||
regex field x = Fix (Regex field x)
 | 
			
		||||
 | 
			
		||||
(.&&.) :: Query -> Query -> Query
 | 
			
		||||
a .&&. b = Fix (And a b)
 | 
			
		||||
 | 
			
		||||
@@ -59,3 +64,6 @@ evaluate = \case
 | 
			
		||||
  Regex Url x -> \BuukaEntry{url=URL u} -> u =~ x
 | 
			
		||||
  Regex Title x -> \BuukaEntry{title=t} -> maybe False (=~ x) t
 | 
			
		||||
  And a b -> \e -> a e && b e
 | 
			
		||||
 | 
			
		||||
predicate :: Query -> BuukaEntry -> Bool
 | 
			
		||||
predicate = cata evaluate
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
module Operations
 | 
			
		||||
  ( module Operations.Insert
 | 
			
		||||
  , module Operations.List
 | 
			
		||||
  , module Operations.Query
 | 
			
		||||
  )
 | 
			
		||||
  where
 | 
			
		||||
 | 
			
		||||
@@ -8,3 +9,5 @@ import Operations.Insert
 | 
			
		||||
       (insert)
 | 
			
		||||
import Operations.List
 | 
			
		||||
       (list)
 | 
			
		||||
import Operations.Query
 | 
			
		||||
       (query)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								src/Operations/Query.hs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/Operations/Query.hs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
module Operations.Query where
 | 
			
		||||
 | 
			
		||||
import Data.Query
 | 
			
		||||
 | 
			
		||||
import Control.Monad.Buuka
 | 
			
		||||
import Control.Monad.Reader
 | 
			
		||||
 | 
			
		||||
import Data.Foldable
 | 
			
		||||
       (traverse_)
 | 
			
		||||
 | 
			
		||||
import Data.Buuka
 | 
			
		||||
       (Buuka)
 | 
			
		||||
import qualified Data.Buuka as B
 | 
			
		||||
 | 
			
		||||
import Operations.Format
 | 
			
		||||
 | 
			
		||||
query :: Field String -> String -> BuukaM ()
 | 
			
		||||
query field q =
 | 
			
		||||
  buukaQ (asks go) >>= traverse_ (liftIO . putStrLn)
 | 
			
		||||
  where
 | 
			
		||||
    go :: Buuka -> [String]
 | 
			
		||||
    go b = formatEntries b (filter (predicate (regex field q)) . B.elements $ b)
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user