cookbook 0.1.3.1 → 0.1.4.0
raw patch · 5 files changed
+129/−3 lines, 5 files
Files
- Cookbook/Common.hs +6/−1
- Cookbook/Recipes/Detect.hs +41/−0
- Cookbook/Recipes/Groups.hs +23/−0
- Cookbook/Recipes/Sanitize.hs +57/−0
- cookbook.cabal +2/−2
Cookbook/Common.hs view
@@ -1,4 +1,4 @@-module Cookbook.Common(sub,positions,pos,apply) where+module Cookbook.Common(sub,positions,pos,apply,flt) where --Cut a list off at a certain point --sub [1,2,3] 1 -> [2 3]@@ -29,5 +29,10 @@ apply :: [(a -> a)] -> a -> a apply [] c = c apply (f:fs) c = apply fs (f c)++-- | Flatten a list one level.+flt :: [[a]] -> [a]+flt [] = []+flt (x:xs) = x ++ flt xs
+ Cookbook/Recipes/Detect.hs view
@@ -0,0 +1,41 @@+module Cookbook.Recipes.Detect(represent,toRepex,strpex,strmatch,containingPattern,withPattern) where++import Data.Maybe++import Cookbook.Common+import Cookbook.Ingredients.Lists.Access+import Cookbook.Ingredients.Functional.Break++-- | Represent a list using symbols, and if it's not found, return Nothing.+represent :: (Eq a) => [([a],b)] -> a -> (Maybe b)+represent [] _ = Nothing+represent ((a,b):c) item+ | item `elem` a = (Just b)+ | otherwise = represent c item++-- | Filter maybes out, replacing Nothings with a failsafe "catch-all"+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)++--Standardized functions++-- | Standard interface to "toRepex" for strings.+strpex :: String -> String+strpex x = toRepex [(['a'..'z'],'@'),(['A'..'Z'],'!'),(['0'..'9'],'#'),([':'..'@']++['\\'..'`']++[' '..'/'],'&')] x '_'++-- | Does the string contain the standard strpex pattern?+strmatch :: String -> String -> Bool+strmatch x c = (strpex x) `contains` c++-- | All lines containing this pattern+containingPattern :: [String] -> String -> [String]+containingPattern x c = filter (flip strmatch c) x++-- | All patterns matching the pattern+withPattern :: [String] -> String -> [String]+withPattern [] _ = []+withPattern a@(x:xs) c+ | strpex takeX == c = takeX : withPattern xs c+ | otherwise = withPattern xs c+ where takeX = (take (length c) x)+
+ Cookbook/Recipes/Groups.hs view
@@ -0,0 +1,23 @@+--Groups is a very small markup language with an inflexible syntax.+--Groups have data constructors and implementations.+--An example of a groups file can be found in the Examples directory of this repo+module Cookbook.Recipes.Groups(Constructor,construct,implement) where++import Cookbook.Ingredients.Lists.Modify+import Cookbook.Ingredients.Lists.Access+import Cookbook.Ingredients.Tupples.Look+import Cookbook.Recipes.Sanitize+import Cookbook.Continuous++type Constructor = (String,[String])++--Example: phone:number,name+-- | Construct a data constructor. Delmits using '_'+construct :: String -> Constructor+construct x = ((before x ':'),(splitOn (after x ':') '_'))++--Example: phone:123-456-7899,Joe+-- | Using a list of constructors, implements an instantiation.+implement :: String -> [Constructor] -> [(String,String)]+implement x c = zip typ (splitOn (after x ':') ',')+ where typ = case look c (before x ':') of (Just e) -> e; _ -> error "Type undefined."
+ Cookbook/Recipes/Sanitize.hs view
@@ -0,0 +1,57 @@+module Cookbook.Recipes.Sanitize(blacklist,rmleading,up,down,rmdb,rmdbAll,tolower,toupper,rmlws,rmsymbols) where+--"Sanization" is quite the overloaded term. The purpose of this sanitzation lib+--is to be as unobtrusive yet flexible as possible. This will generally work with+--strings, so some domain-specific solutions for strings are present in this lib+import Cookbook.Common+import Cookbook.Ingredients.Lists.Modify+import Cookbook.Ingredients.Lists.Access+import Cookbook.Ingredients.Functional.Break++--Generic Underpinnings++-- | Remove each element of the second list from the first.+blacklist :: (Eq a) => [a] -> [a] -> [a]+blacklist x c = apply (map (flip rm) c) x++-- | Remove leading elements from the list. Useful for getting rid of spaces.+rmleading :: (Eq a) => [a] -> a -> [a]+rmleading x c = filterBreak (==c) x++-- | Move an element in one list to the other, if it is not there already.+up :: (Eq a) => ([a],[a]) -> [a] -> [a]+up (a,b) c = map (refpos (a,b)) c++-- | Up, but down. If that helps.+down :: (Eq a) => ([a],[a]) -> [a] -> [a]+down (a,b) c = map (refpos (b,a)) c++-- | Remove all adjacent doubles from the list.+rmdb :: (Eq a) => [a] -> [a]+rmdb [] = []+rmdb [x] = [x]+rmdb (x:y:zs) = if x == y then x: rmdb (removeBreak (== x) (y:zs)) else x : rmdb (y:zs)++-- | Removes absolutely all doubles, leaving the first copy.+rmdbAll :: (Eq a) => [a] -> [a]+rmdbAll [] = []+rmdbAll (x:xs) = x : rmdb (rm xs x)++--Interface to generic underpinnings++-- | Moves a string to lower case.+tolower :: String -> String+tolower = down (['a'..'z'],['A'..'Z'])++-- | Moves a string to upper case.+toupper :: String -> String+toupper = up (['a'..'z'],['A'..'Z'])++-- | Removes leading whitespace.+rmlws :: String -> String+rmlws = ((flip rmleading) ' ')++-- | Removes all symbols, leaving spaces intact.+rmsymbols :: String -> String+rmsymbols = ((flip blacklist) (['\\'..'`']))++
cookbook.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.1.3.1+version: 0.1.4.0 -- A short (one-line) description of the package. synopsis: An independent library of common haskell operations.@@ -47,7 +47,7 @@ library -- Modules exported by the library.- exposed-modules: Cookbook.Common, Cookbook.Continuous, Cookbook.IO, Cookbook.Recipes.DiffStat, Cookbook.Recipes.Configuration, Cookbook.Ingredients.Functional.Break, Cookbook.Ingredients.Lists.Access, Cookbook.Ingredients.Lists.Modify, Cookbook.Ingredients.Tupples.Look+ exposed-modules: Cookbook.Common, Cookbook.Continuous, Cookbook.IO, Cookbook.Recipes.DiffStat, Cookbook.Recipes.Configuration, Cookbook.Ingredients.Functional.Break, Cookbook.Ingredients.Lists.Access, Cookbook.Ingredients.Lists.Modify, Cookbook.Ingredients.Tupples.Look, Cookbook.Recipes.Detect, Cookbook.Recipes.Groups, Cookbook.Recipes.Sanitize -- Modules included in this library but not exported. -- other-modules: