--Cookbook.Recipes.Detect
module Cookbook.Recipes.Detect(represent,toRepex,strpex,strmatch,containingPattern,withPattern) where
import Data.Maybe
import qualified Cookbook.Ingredients.Lists.Access as Ac
represent :: (Eq a) => [([a],b)] -> a -> (Maybe b)
represent [] _ = Nothing
represent ((a,b):c) item = if item `elem` a then (Just b) else represent c item
toRepex :: (Eq a) => [([a],b)] -> [a] -> b-> [b]
toRepex a b failsafe= map (\c -> case c of (Just x) -> x;_ -> failsafe) (map (represent a) b)
strpex :: String -> String
strpex x = toRepex [(['a'..'z'],'@'),(['A'..'Z'],'!'),(['0'..'9'],'#'),([':'..'@']++['\\'..'`']++[' '..'/'],'&')] x '_'
strmatch :: String -> String -> Bool
strmatch x c = (strpex x) `Ac.contains` c
containingPattern :: [String] -> String -> [String]
containingPattern x c = filter (flip strmatch c) x
withPattern :: [String] -> String -> [String]
withPattern [] _ = []
withPattern a@(x:xs) c = if strpex takeX == c then takeX : withPattern xs c else withPattern xs c
where takeX = (take (length c) x)