packages feed

cookbook 2.1.0.0 → 2.1.2.0

raw patch · 16 files changed

+260/−112 lines, 16 files

Files

Cookbook/Essential/Common.hs view
@@ -1,29 +1,33 @@---Cookbook.Essential.Common---Common is the root of the Cookbook library. It provides the abstracts commonly used to build---robust applications and sub-libraries within Cookbook. In any Cookbook implementation, Common---varies based on the language features the language presents. In Haskell, Common merely provides---informational contexts for modifying/accessing lists. They are placed in this high of a module because---of how ubiquitous their use is, even though they fit in with Cookbook.Ingredients.Lists+{- |+   Module      :   Cookbook.Essential.Common+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Standalone - ghc) +"global" functions for the entirety of the Cookbook library.+Common is the potpourri of Cookbook, with no category except "everything uses me!"+-} module Cookbook.Essential.Common where --- | Returns a new list starting at a position. List positions start at 0.+-- | Return a list starting at an index. Indices start at 0. sub :: (Eq a) => [a] -> Int -> [a] sub [] _ = [] sub x 0 = x sub (x:xs) c = sub xs (c - 1) --- | Finds every occurence of an element within a list, starting at position 0.+-- | Find the occurrences of an element in a list.  positions :: (Eq a) => [a] -> a -> [Int] positions x c = let y = zip x [0..(length x)] in find y   where find y = [e | (d,e) <- y, d == c] --- | Interface to position for finding the position of the first occurence in a list.+-- | C-style wrapper for positions. Returns the first occurrence in a list, or -1 on notElem. pos :: (Eq a) => [a] -> a -> Int pos x c | c `notElem` x = -1 pos x c = let ans = positions x c in ((if (length ans) > 1 then (head . tail) else head) ans) --- | Apply a list of functiosn to one value, using the result from the function beforehand.+-- | Reversal of map function. Chains calls of functions over a parameter, starting with head. apply :: [(a -> a)] -> a -> a apply [] c = c apply (f:fs) c = apply fs (f c)@@ -33,6 +37,7 @@ flt [] = [] flt (x:xs) = x ++ flt xs +-- | Execute a function from the end of a list to the front. fromLast :: ([a] -> [a]) -> [a] -> [a] fromLast f c = rev $ f $ rev c   where rev [] = []
Cookbook/Essential/Continuous.hs view
@@ -1,19 +1,34 @@ {-# LANGUAGE FlexibleInstances     #-}+ {-# LANGUAGE MultiParamTypeClasses #-} ---Cookbook.Essential.Continuous---Continuous is the most important Cookbook library. It defines Overloaded Generic Interfaces--- (refered to as OGI's throughout the library) for working on two generic types of data, usually---   lists and items.+{- |+   Module      :   Cookbook.Essential.Continuous+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Unstable+   Portability :   Portable (Cookbook)++Library for overloading functions across lists and singular items, as well as tupples.+Somewhat abuses FlexibleInstance and Typeclasses.+-} module Cookbook.Essential.Continuous where  import qualified Cookbook.Essential.Common             as Cm import qualified Cookbook.Ingredients.Functional.Break as Br import qualified Cookbook.Ingredients.Lists.Access     as Ac +-- | Classifies items that can be modified by either a list or item. class Continuous list part where+  +  -- | Returns all elements after part.   after :: list -> part -> list+  +  -- | Returns all elements after part.   before :: list -> part -> list+  +  -- | Removes part from the list.   delete :: list -> part -> list  instance (Eq a) => Continuous [a] a where@@ -31,8 +46,10 @@   delete [] _ = []   delete x c  = if not (x `Ac.contains` c) then x else  (before x c) ++ delete (after x c) c --- Spliceable+-- | Classifies information which can be split by a tupple. class Splicable a b where+  +  -- | Removes everything between the tupple's parameters, including the parameters themselves.   splice :: a -> b -> a  instance (Eq a) => Splicable [a] (a,a) where@@ -41,8 +58,10 @@ instance (Eq a) => Splicable [a] ([a],[a]) where   splice ls (a,b)  = before ls a ++ after (after ls a) b --- Replacable+-- | Classifies data which can be replaced. class Replacable list repls where+  +  -- | Replaces part of list.   replace  :: list -> repls -> list    instance (Eq a) => Replacable [a] (a,a) where@@ -60,8 +79,9 @@ instance (Eq a) => Replacable [a] [([a],[a])] where   replace x c = Cm.apply (map (flip replace) c) x --- Removable+-- | Classifies data which can be removed from a list. class Removable list toRm where+  -- | Remove data from a list.   remove :: list -> toRm -> list  instance (Eq a) => Removable [a] a where@@ -71,7 +91,3 @@   remove [] _       = []   remove x@(a:b) c  = if take (length c) x == c then remove (restart x) c else a : remove b c     where restart d = Cm.sub d (length c)--instance (Eq a) => Removable [a] (a,a) where-  remove [] _ = []-  remove (x:xs) (a,b) = if and [x == a, b `elem` xs] then remove (tail (Br.removeBreak (/=b) xs)) (a,b) else x : remove xs (a,b)
Cookbook/Essential/IO.hs view
@@ -1,40 +1,44 @@---Cookbook.Essential.IO---IO is the only Cookbook library to import System modules. As the name states, Cookbook.IO makes IO---easier and less error-prone by wrapping common IO "gotchas" in a function.+{- |+   Module      :   Cookbook.Essential.IO+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Non-Portable (Cookbook, Strict, Environment)++Library for completing common IO tasks, integrating with files and UNIX functions.+-}+ module Cookbook.Essential.IO where  import qualified System.IO                         as LIO import qualified System.IO.Strict                  as SIO+import qualified Cookbook.Essential.Common         as Cm import qualified Cookbook.Essential.Continuous     as Ct import qualified Cookbook.Ingredients.Lists.Modify as Md  import System.Environment        import System.Directory +-- | Return the lines of a file as a list of Strings. filelines :: String -> IO ([String])-filelines x = do-  y <- LIO.openFile x LIO.ReadMode-  yc <- SIO.hGetContents y-  return (lines yc)+filelines x = fmap lines $ LIO.openFile x LIO.ReadMode >>= SIO.hGetContents +-- | Prompts the user for keyboard input prompt :: String -> IO (String) prompt x = do     putStr x     LIO.hFlush LIO.stdout     getLine +-- | Returns the path of a file in the user's home directory.  inhome :: String -> LIO.IOMode -> IO (LIO.Handle)-inhome x  c = do-  home <- getHomeDirectory-  LIO.openFile (home ++ x) c--getHomePath :: String -> IO (String)-getHomePath x = do-  home <- getHomeDirectory-  return (home ++ x)+inhome x c = fmap ((++x).(++"/")) getHomeDirectory >>= (flip LIO.openFile) c +-- | Pure. Returns the file name with the directory truncated. filename :: String -> String-filename = Md.rev . ((flip Ct.before) '/') . Md.rev+filename = Cm.fromLast ((flip Ct.before) '/') +-- | Pure. Returns the module name. That is, path to the file with the file cut off. modulename :: String -> String-modulename = Md.rev . ((flip Ct.after) '/') . Md.rev+modulename = Cm.fromLast ((flip Ct.after) '/')
Cookbook/Ingredients/Functional/Break.hs view
@@ -1,27 +1,30 @@---Cookbook.Ingredients.Functional.Break---Break contains utilities for collectings lists conditionally. Unlike filter ---from prelude, breaks will stop collecting elements of the list at the first---untrue value. As an addition to this library are functions relating to---mapping predicates over lists to yield either a different result or one solution.+{- |+   Module      :   Cookbook.Ingredients.Functional.Break+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Standalone - ghc) +Library for breaking conditionally on lists. When one filters a list, it will filter all of the elements. When one filterBREAKS a list, it will stop collecting the list at a desired element. This library also includes functions for conditionally transforming a list.+-} -- I Always imagine this library being really mad. FilterBREAK SMASH! RAAAAGH+ module Cookbook.Ingredients.Functional.Break where +-- | Drop a list until a predicate yields false, returning the false item and the rest of the list. removeBreak :: (a -> Bool) -> [a] -> [a] removeBreak _ [] = [] removeBreak f (c:cs) = if not $ f c then (c:cs) else removeBreak f cs +-- | Collect a list until a predicate yields false for a value. filterBreak :: (a -> Bool) -> [a] -> [a] filterBreak _ [] = [] filterBreak f (c:cs) = if not $ f c then [] else c : filterBreak f cs +-- | Returns true if any element in the list yields true for a predicate. imbreak :: (a -> Bool) -> [a] -> Bool imbreak f = or . map f +-- | Conditionally transform a list. If a predicate returns true, use lval. Otherwise, use rval. btr :: (a -> Bool) -> (b,b) -> [a] -> [b] btr f (a,b) = map (\c -> if f c then a else b)--splitBool :: (Eq a) => (a -> Bool) -> [a] -> [[a]]-splitBool _ [] = []-splitBool f c = filter (/= []) (fP: if fP /= [] then (splitBool f tP) else [])-  where fP = filterBreak f c-        tP = removeBreak f c
Cookbook/Ingredients/Lists/Access.hs view
@@ -1,39 +1,52 @@---Cookbook.Ingredients.Lists.Access---Access is a library for generating statistics about a list. The lists are not changed as a result---of executing any Access function.-+{- |+   Module      :   Cookbook.Ingredients.Lists.Access+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Cookbook)+Library for accessing the information from a list. Modify and Access are six in one and half-dozen in the other in a purely functional language, but the overall theme is this: Access is for functions which return portions of the list, or information about a list. Modify is the library which transforms a list.+-} module Cookbook.Ingredients.Lists.Access where  import qualified Cookbook.Ingredients.Functional.Break as Br import qualified Cookbook.Ingredients.Tupples.Assemble as As import qualified Cookbook.Essential.Common             as Cm +-- | Counts the occurrences an element has within a list. count :: (Eq a) => [a] -> a -> Int count x c = sum $ Br.btr (==c)  (1,0) x +-- | Checks to see if a list is a sub-list of the list. contains :: (Eq a) => [a] -> [a] -> Bool contains [] _ = False contains x c = if part x == c then True else contains (tail x) c   where part = take (length c) -qsort :: (Ord a) => [a] -> [a]+-- | QuickSort implementation. Sorts a list of data quickly?+qsort :: (Ord a) => [a] -> [a] -- AKA Kenyan Sort qsort [] = [] qsort (x:xs) = lessT ++ [x] ++ greatT   where  (lessT,greatT)  = (qsort $ filter (<=x) xs,qsort $filter (>x) xs) +-- | Safe implementation of !!. Uses maybe instead of error. pull :: [a] -> Int -> Maybe a pull _ c | c < 0 = Nothing pull [] _  = Nothing pull (x:xs) c = if c == 0 then (Just x) else pull xs (c - 1) +-- | Referrential positioning. Find the position of an element in the first list, and return the element from the second list of the same position. In the event that the second list is shorter than the position where the element is found in the first list, it returns the parameter.  refpos :: (Eq a) => ([a],[a]) -> a -> a refpos (a,b) c = let d = (pull b (Cm.pos a c)) in case d of (Just x) -> x;(Nothing) -> c +-- | Test to make sure that all elements in a list are equal to a value. areAll :: (Eq a) => [a] -> a -> Bool areAll x c = not $ Br.imbreak (/=c) x +-- | Re-implementation of isPrefixOf for some reason. Tests to see if a list is the first part of antoher list. isBefore :: (Eq a) => [a] -> [a] -> Bool isBefore li tf = take (length tf) li == tf +-- | Test to see if a list is surrounded by an item. surrounds :: (Eq a) => (a,a) -> [a] -> Bool surrounds (b,c) a = (head a, last a) == (b,c)
Cookbook/Ingredients/Lists/Modify.hs view
@@ -1,49 +1,50 @@---Cookbook.Ingredients.Lists.Modify---Library for altering the contents of a list.-+{- |+   Module      :   Cookbook.Ingredients.Lists.Modify+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Cookbook)+Library for modifying data within a list, and transforming lists in certain ways. While that's vague, so is the definition of "To Modify". +-} module Cookbook.Ingredients.Lists.Modify where -import qualified Cookbook.Essential.Continuous         as Cnt import qualified Cookbook.Essential.Common             as Cm+import qualified Cookbook.Essential.Continuous         as Cnt import qualified Cookbook.Ingredients.Functional.Break as Br import qualified Cookbook.Ingredients.Lists.Access     as Ac +-- | Reverses a list. rev :: [a] -> [a] rev [] = [] rev (x:xs) = rev xs ++ [x] +-- | Removes all occurances of an element from a list. See MDN1 in the source for a run-down on why it's implemented here and in Continuous. rm :: (Eq a) => [a] -> a -> [a] -- See MDN1 rm x c = filter (/=c) x +-- | Splits a list on an element, making a list of lists based on the element as a seperator. splitOn :: (Eq a) => [a] -> a -> [[a]] splitOn [] _ = [] splitOn x c = if (c `notElem` x) then [x] else (Cnt.before x c) : splitOn (Cnt.after x c) c -snipe :: (Eq a) => [a] -> (a,Int) -> [a]-snipe x (t,c) = (take c x) ++ [t] ++ (Cm.sub x (c + 1))--insert :: (Eq a) => [a] -> ([a],Int) -> [a]-insert x (t,c) = (take c x) ++ t ++ (Cm.sub x c)-+-- | Returns the data between two items. between :: (Eq a) => [a] -> (a,a) -> [a] between a (c,d) = Cnt.after (take (last $ Cm.positions a d) a) c +-- | Implementation of between that works on a list of lists, and using Contains rather than elem. linesBetween :: (Eq a) => [[a]] -> ([a],[a]) -> [[a]] linesBetween a (c,d) = tail $ Br.filterBreak (\e -> not $ Ac.contains e d) $ Br.removeBreak (\e -> not $ Ac.contains e c) a +-- | Put an element after every element of a list, not including the last element. intersperse :: [a] -> a -> [a] intersperse [x] _ = [x] intersperse (x:xs) c =  x:c : intersperse xs c -onLast :: (a -> a) -> [a] -> [a]-onLast _ []     = []-onLast f [x]    = f x : []-onLast f (x:xs) = x : onLast f xs--(?) :: (a -> b) -> (a -> b) -> Bool -> (a -> b)-(?) f1 f2 it = (if it then f1 else f2)-+(?) :: a -> a -> Bool -> a+(?) a b c = if c then a else b +-- | Returns a list of all elements surrounded by elements. Basically a between which works more than once. surroundedBy :: (Eq a) => [a] -> (a,a) -> [[a]] -- Map checks if a or b are not elememts. surroundedBy x (a,b) = if or (map (not . (flip elem) x) [a,b]) then [] else recurse   where recurse = Cnt.before (Cnt.after x a) b : surroundedBy (Cnt.after x b) (a,b)
Cookbook/Ingredients/Lists/Stats.hs view
@@ -3,10 +3,22 @@ import qualified Cookbook.Ingredients.Lists.Access     as Ac import qualified Cookbook.Ingredients.Lists.Modify     as Md import qualified Cookbook.Ingredients.Tupples.Assemble as As-import qualified Cookbook.Recipes.Sanitize              as Sn+import qualified Cookbook.Recipes.Sanitize             as Sn+import qualified Cookbook.Recipes.Math                 as Ma  frequency :: (Eq a) => [a] -> [(a,Int)] -frequency x = let y = map (\c -> (c,Ac.count x c)) x in Sn.rmdbAll y+frequency x = let y = map (\c -> (c,Ac.count x c)) x in As.rmDb y  mostFrequent :: (Eq a) => [a] -> Int -> [a] mostFrequent x c = take c $ Md.rev (As.assemble  $ frequency x)++wordscore :: (Eq a) => [a] -> [a] -> Double+wordscore a b = (freqScore a b - 0.1) + (0.1 / realToFrac (if diffLen == 0 then 1 else diffLen))+  where diffLen = (abs $(length a) - (length b))++freqScore :: (Eq a) => [a] -> [a] -> Double+freqScore a b =  (rawFreq / (fromIntegral diffLen))+  where diffLen = fromIntegral (if (length (frequency a)) < (length (frequency b)) then length (frequency a) else length (frequency b))+        rawFreq = fromIntegral ((sum $ (map (\e -> if e `elem` d then 1 else 0) c)))+        (c:d:_) = map ((flip mostFrequent) diffLen) [a,b] +
Cookbook/Ingredients/Tupples/Assemble.hs view
@@ -1,15 +1,28 @@---Cookbook.Ingredients.Tupples.Assemble---Assemble is a library for sorting and constructing lists from touples with a member of Ord in the second slot.---It is used for frequency analyzation, as well as other very particular tasks, but is kept out of Recipes because it is still generic.-module Cookbook.Ingredients.Tupples.Assemble(tupsort,assemble) where+{- |+   Module      :   Cookbook.Ingredients.Tupples.Assemble+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Standalone - ghc) --- | Quicksort a list of tupples, with the (Ord) element being the second one.+Library for arranging and modifying lists of twopples. Dubber Tuppers? There has to be a better word for this. It works on two-sided Tupples.+-}++module Cookbook.Ingredients.Tupples.Assemble where++-- | Sorts a list of Tupples based on their second element. tupsort :: (Ord b) => [(a,b)] -> [(a,b)] tupsort [] = [] tupsort ((a,n):xs) = lesser ++ [(a,n)] ++ greater   where lesser   = tupsort [(c,d) | (c,d) <- xs, d <= n]         greater = tupsort [(c,d) | (c,d) <- xs, d > n] --- | Orders element-Ord tupples and gets all of the first elements.+-- | Order a list of tupples by their rval, and collect the lvals as a list. assemble :: (Ord b) => [(a,b)] -> [a] assemble = (map fst) . tupsort++-- | Removes all double-entries from a list of tupples, contingent on just lval.+rmDb :: (Eq a) =>[(a,b)] -> [(a,b)]+rmDb [] = []+rmDb ((a,b):c) = (a,b) : rmDb [(d,e) | (d,e) <- c, d /= a]
Cookbook/Ingredients/Tupples/Look.hs view
@@ -1,27 +1,35 @@---Cookbook.Ingredients.Tupples.Look---This library is for manipulating and searching lists of two-element tupples.+{- |+   Module      :   Cookbook.Ingredients.Tupples.Look+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Standalone - ghc) +Library for using tupples as an associative list. It can be thought of like a "map" in Clojure, or a database of key-value pairs. This is also the general-purpose tupple library, so there's stragglers.+-}+ module Cookbook.Ingredients.Tupples.Look(look,lookList,swp,rmLook,group) where --- | Returns the second element of the first tupple where the first element matches input.+-- | Look up an lval in a list of tupples. look :: (Eq a) => [(a,b)] -> a -> (Maybe b) look [] _ = Nothing look (a:b) c = if fst a == c then Just (snd a) else look b c --- | Returns all second elements where (fst t) matches the input.+-- | Returns all elements where the lval matches the element. lookList :: (Eq a) => [(a,b)] -> a -> [b] lookList a b = map snd $ filter (\(c,d) -> c == b) a --- | Swap the order of a second-degree tupple.+-- | Swap the lval with the rval, and vice-versa. swp :: (a,b) -> (b,a) swp    (a,b)  = (b,a) --- | Reverse this.look. Will remove any matches.+-- | Removes all matches of the lval. rmLook :: (Eq a) => [(a,b)] -> a -> [(a,b)] rmLook [] _ = [] rmLook ((a,b):cs) d = if a == d then cs else (a,b) : rmLook cs d --- | Turn a list into as many bifold tupples as possible.+-- | Turns a list into as many double-tupples (a,a) as it can, dropping items from uneven lists. group :: [a] -> [(a,a)] group [] = [] group [x] = []
Cookbook/Project/Configuration/Configuration.hs view
@@ -1,8 +1,17 @@---Cookbook.Project.Configuration.Configuration (Cf)--- Cf is a library for reading very simple configuration files.+{- |+   Module      :   Cookbook.Project.Configuration.Configuration+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Cookbook)+Configuration is a library for simple configuration files. It's pretty fragile, but it's been used for some pretty majorly used scripts, namely espion. The lvalue becomes the fst of a name-value pair, and the rvalue becomes the second, with the syntax: lvalue : rvalue\n+-}+ module Cookbook.Project.Configuration.Configuration where import qualified Cookbook.Ingredients.Lists.Modify as Md import qualified Cookbook.Ingredients.Tupples.Look as Lk +-- | Read the lines of a configuration file, query it, and return an answer to the query. conf :: [String] -> String -> String conf x c = let configs = [let (d:f:_) = (Md.splitOn y ':') in (d,f)| y <- x, (length y) > 2, ':' `elem` y] in case (Lk.look configs c) of (Just f) -> f;(Nothing) -> []
Cookbook/Project/Preprocess/Preprocess.hs view
@@ -1,5 +1,12 @@---Cookbook.Project.Preprocess.Preprocess---A library for implementing preprocess syntax, as defined in the .std+{- |+   Module      :   Cookbook.Project.Preprocess.Preprocess+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Cookbook+A library for preprocessing information using replacing sweeps. Supports inline comments. Does not currently support whitespace-insignificant parsing.+-}  module Cookbook.Project.Preprocess.Preprocess where @@ -7,13 +14,16 @@ import qualified Cookbook.Essential.Common          as Cm import qualified Cookbook.Essential.Continuous      as Ct +-- | Binds possibly multiple inputs on one line to one input. General syntax is: inp1|inp2_out makeParams :: String -> [(String,String)] makeParams x = (flip zip) (repeat lastPart) $ Md.splitOn firstPart '|'   where (firstPart:lastPart:[]) = Md.splitOn x '_' +-- | Sanitizes the strings before parsing. sanitize :: String -> String-sanitize x = Ct.remove x ('$','$')+sanitize x = Ct.splice x ('$','$') +-- | Generate Program Language. Generates a list of input-output pairs to be replaced. gPL :: [String] -> [(String,String)] gPL x = Cm.flt $ map makeParams sanitized   where sanitized = Ct.remove (map (\c -> if (length c) > 3 then (sanitize c) else "") x) ""
Cookbook/Project/Quill/Quill.hs view
@@ -1,3 +1,13 @@+{- |+   Module      :   Cookbook.Project.Quill.Quil+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Cookbook+A library for reading simple databases. As what was originally a quirk of the library, database tables can be split up and still parsed as if they were a unit, giving it the ability to read the same table from multiple files in parallel. Quill supports a comment syntax, whitespace-insignificant parsing, and a full CRUD API. Quill will EVENTUALLY be replaced by Scribe2, but the planning for Scribe2 has not yet begun.+-}+ module Cookbook.Project.Quill.Quill where   import qualified Cookbook.Essential.Common         as Cm@@ -7,58 +17,70 @@ import qualified Cookbook.Ingredients.Tupples.Look as Lk import qualified Cookbook.Recipes.Sanitize         as Sn ---Custom data+-- | A table is a record of a name and the information within it. data Table = Table {name :: String, info :: [(String,String)]} --- Parsing+-- | Sanitizes strings for Quill processing. Removes comments, newlines, and flattens it into a string. prepare :: [String] -> String prepare = ((flip Ct.splice) ("(*","*)")) . ((flip Sn.blacklist) ['\n']) . Cm.flt +-- | Splits a list into tupples. tokenize :: [a] -> [(a,a)] tokenize []  = [] tokenize [x] = [] tokenize (x:y:xs) = (x,y) : tokenize xs --- Table access+-- | Returns the names of all tables in the file. tblNames :: String -> [String] tblNames x = (Ct.before x '{') : (tail $ Md.surroundedBy (Sn.blacklist x [' '])  ('}','{')) +-- | *Should not be used raw except for API programming. Really shouldn't be included, but too lazy to enumerate top-level declarations for selective export* Returns all entry lines from within tables in database. headlessData :: String -> [[String]] headlessData x = map ((flip Md.splitOn) ';') $  (Md.surroundedBy x ('{','}')) +-- | Returns all of the tokens from headlessData. tokenLists :: [[String]] -> [[(String,String)]] tokenLists x =  ( map (map (\c -> (Ct.before c ':',Ct.after c ':'))) x)  -- API functions+-- | Creates a listing of all tables in the file. tables :: [String] -> [(String,[(String,String)])] tables x = let y = prepare x in    zip (tblNames y) $ tokenLists (headlessData y) +-- | Gets a particular table in the file, returning its key-value pairs. getTable :: [(String,[(String,String)])] -> String -> [(String,String)] getTable x c = Cm.flt $ Lk.lookList x c +-- | Gets the particular item within a table from a database. lookUp :: [(String,[(String,String)])] -> (String,String) -> [String] lookUp x (c,d) = ((flip Lk.lookList) d) $ getTable x c +-- | Creates a new table within the database. createTable :: [(String,[(String,String)])] -> String -> [(String,[(String,String)])] createTable x c = (c,[]) : x +-- | Removes an entire table from the database by name. removeTable :: [(String,[(String,String)])] -> String -> [(String,[(String,String)])] removeTable x c = [e | e@(d,_) <- x, d /= c] +-- | Removes a particular item from a table within a database. removeItem :: [(String,[(String,String)])] -> (String,String) -> [(String,[(String,String)])] removeItem [] _ = [] removeItem ((tbl,ite):re) (tb,it)   | tbl == tb = (tbl,filter (\(c,d) -> c /= it) ite) : removeItem re (tb,it)   | otherwise = (tbl,ite) : removeItem re (tb,it)-                ++-- | Adds an item to a table within a database. addItem :: [(String,[(String,String)])] -> String -> (String,String) -> [(String,[(String,String)])] addItem [] _ _ = [] addItem ((a,b):c) tb it = if a == tb then (a,it:b) : addItem c tb it else (a,b) : addItem c tb it +-- | Changes an item within a table. The identifier will remain the same; only the rvalue will change. changeItem :: [(String,[(String,String)])] -> (String,String) -> String -> [(String,[(String,String)])] changeItem db (tb,it) c = addItem (removeItem db (tb,it)) tb (it,c) +-- | Basically a toString function for Quill tables. It turns data into a String format which can be parsed by the Quill parsing stack. tableToString :: (String,[(String,String)]) -> String tableToString (x,b) = x ++ "{" ++ (Cm.flt (map detokenize b)) ++ "}"   where detokenize (a,b) = a ++ ":" ++ b ++ ";"
Cookbook/Recipes/Algorithm.hs view
@@ -1,25 +1,37 @@---Cookbook.Recipes.Algorithm---Used for traversing data structures.+{- |+   Module      :   Cookbook.Recipes.Algorithm+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Cookbook)+Library for interacting with high-evel data structures found in Cookbook.Recipes.DataStructures (Ds). It also implements some data types that would have just been type synonyms if implemented in Ds.+-}+ module Cookbook.Recipes.Algorithm where  import Cookbook.Recipes.DataStructures import Cookbook.Ingredients.Lists.Modify +-- | Get every node of a tree, put it into a list. climb :: (Tree a) -> [a] climb x = case x of (Empty) -> [];(Branch a (Empty) b) -> a : climb b;                     (Branch a b (Empty)) -> a : climb b;                     (Branch a b d) -> a : climb b ++ climb d  +-- | Apply a function to every node in a tree. treeMap :: (Tree a) -> (a -> a) -> (Tree a) treeMap tr f = case tr of Empty -> Empty;                           (Branch a (Empty) z@(Branch c _ _)) -> (Branch (f c) (treeMap z f) (Empty));                           (Branch a z@(Branch b _ _) (Empty)) -> (Branch (f b) (Empty) (treeMap z f)) +-- | Collectively nullify nodes on a tree. treeFilter :: (Tree a) -> (a -> Bool) -> (Tree a) treeFilter Empty f = Empty treeFilter (Branch a (Empty) z@(Branch b _ _)) f = if (f a) then (Branch a (Empty) (treeFilter z f)) else Empty treeFilter (Branch a z@(Branch b _ _) (Empty)) f = if (f a) then (Branch a (treeFilter z f) (Empty)) else Empty +-- | Generates a list of points, with the specified null data in the snd of the tupples. genMatrix :: (Int,Int) -> a -> [((Int,Int),a)] genMatrix (a,b) c = helper (0,0)   where helper (d,e)
Cookbook/Recipes/DataStructures.hs view
@@ -1,7 +1,16 @@---Cookbook.Recipes.DataStructures---Higher-level data structures for use in the rest of Cookbook.+{- |+   Module      :   Cookbook.Recipes.DataStructures+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Standalone - ghc)+Library for defining high-level generic data structures, most commonly containers. Specialized data types for specialized projects should go into Projects. It's sort of why it exists.+-}+ module Cookbook.Recipes.DataStructures where +-- | Implementation of a binary tree. data Tree a = Empty | Branch a (Tree a) (Tree a) deriving (Show)--add a "map" and "filter" function for Tree.  
Cookbook/Recipes/Math.hs view
@@ -1,25 +1,36 @@---Cookbook.Recipes.Math---A library for mathematical formulas, parsing, and conversions.+{- |+   Module      :   Cookbook.Recipes.Math+   Copyright   :   (c) 2014 by Nate Pisarski+   License     :   BSD3+   Maintainer  :   nathanpisarski@gmail.com+   Stability   :   Stable+   Portability :   Portable (Standalone - ghc)+Library for working with numbers more easily. It includes somewhat lazy functions for when a lambda will clutter code up too much, as well as more involved mathematical formulae. +-}+ module Cookbook.Recipes.Math where ---Simple, helper arithmetic functions.-inc :: (Num a) => a -> a-inc = (+1)+-- | Increase value by one.+inc :: (Enum a) => a -> a+inc = succ -dec :: (Num a) => a -> a-dec x = x - 1 -- Negatives throw off currying+-- | Decrease value by one,+dec :: (Enum a) => a -> a+dec = pred +-- | Multiply a number by itself. sqr :: (Num a) => a -> a sqr x = (x*x) +-- | Find the average of a group of Fractionals. avg :: (Fractional a) => [a] -> a avg x = sum x / (realToFrac $ length x) ---Basic formulae+-- | Find the standard deviation of a list of data. stdev :: (Fractional a, Floating a) => [a] -> a stdev x = sqrt $ diffs / realToFrac (length x)   where diffs = sum $ [sqr $ a - avg x| a <- x] --- Simple factorial, using fold.-fact :: Int -> Int+-- | Factorial, from 1 to point.+fact :: (Num a, Enum a) => a -> a fact x = foldl (*) x [1..dec x]
cookbook.cabal view
@@ -10,7 +10,7 @@ -- PVP summary:      +-+------- breaking API changes --                   | | +----- non-breaking API additions --                   | | | +--- code changes with no API change-version:             2.1.0.0+version:             2.1.2.0  -- A short (one-line) description of the package. synopsis:	Tiered general-purpose libraries with domain-specific applications.@@ -55,4 +55,4 @@      -- Other library packages from which modules are imported.   build-depends:       base ==4.6.*, directory, strict-  +