packages feed

cookbook (empty) → 0.1.0.0

raw patch · 12 files changed

+318/−0 lines, 12 filesdep +basesetup-changed

Dependencies added: base

Files

+ Cookbook/Common.hs view
@@ -0,0 +1,28 @@+module Cookbook.Common(sub,positions,pos) where++--Cut a list off at a certain point+--sub [1,2,3] 1 -> [2 3]+-- | Returns a new list starting at a position. List positions start at 0.+sub :: (Eq a) => [a] -> Int -> [a]+sub [] _ = []+sub x 0 = x+sub (x:xs) c = sub xs (c - 1)++--Find all positions of an element in a list+--positions [1,2,1,2,1,2] 1 -> [ 0 2 4 ]+--Edge cases: [] on notElem+-- | Finds every occurence of an element within a list, starting at position 0.+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 positions to find the first matching element.+--pos [1,2,3] 2 -> 1+--Edge Cases: -1 on notElem (Maybe unused because this is a helper function)+-- | Interface to position for finding the position of the first occurence in a list.+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)+          ++
+ Cookbook/Continuous.hs view
@@ -0,0 +1,31 @@+{-# LANGUAGE FlexibleInstances     #-}+{-# LANGUAGE MultiParamTypeClasses #-}++module Cookbook.Continuous(Continuous(..)) where++import Cookbook.Common+import Cookbook.Ingredients.Functional.Break++-- | Continuous provides an interface for function overloading. Everything automatically qualifies to be a constrained by class continuous, so no anotation is required in any type signatures.+class Continuous list part where+  +-- | After returns a sub-list after the first element or first occurence of a larger list.+  after :: list -> part -> list+  +-- | Before returns a sub-list before either the first occurence of an element or sublist.+  before :: list -> part -> list+  +instance (Eq a) => Continuous [a] a where+  after x c = tail $ removeBreak (/=c) x+  before x c = filterBreak (/=c) x+  +instance (Eq a) => Continuous [a] [a] where+  after [] _ = []+  after x c+    | take (length c) x == c = sub x ((length c))+    | otherwise = after (tail x) c++  before [] _ = []+  before x c+    | take (length c) x == c = []+    | otherwise = (head x) : before (tail x) c
+ Cookbook/IO.hs view
@@ -0,0 +1,10 @@+module Cookbook.IO(filelines) where+import System.IO+import System.Environment++-- | Returns the lines of a file, wrapped in an IO.+filelines :: String -> IO ([String])+filelines x = do+  y <- openFile x ReadMode+  yc <- hGetContents y+  return (lines yc)
+ Cookbook/Ingredients/Functional/Break.hs view
@@ -0,0 +1,39 @@+module Cookbook.Ingredients.Functional.Break(+removeBreak, filterBreak,btr,imbreak) where++--Return the list at the first untrue return from predicate f.+--removeBreak (==1) [1,1,2,3] -> [2,3]+-- | When the predicate returns false, removeBreak returns the rest of the list.+removeBreak :: (a -> Bool) -> [a] -> [a]+removeBreak _ [] = []+removeBreak f (c:cs)+  | not $ f c = (c:cs)+  | otherwise = removeBreak f cs++--Stop consing a list at first untrue return from predicate f.+--filterBreak (==1) [1,1,2,3] -> [1,1]+-- | When the predicate returns false, filterBreak will stop collecting the list.+filterBreak :: (a -> Bool) -> [a] -> [a]+filterBreak _ [] = []+filterBreak f (c:cs)+  | not $ f c = []+  | otherwise = c : filterBreak f cs++-- Immediately break out of execution on a true.+-- Will return false at the end of execution+-- | imbreak will return true if any of the members of the list satisfy the predicate.+imbreak :: (a -> Bool) -> [a] -> Bool+imbreak _ [] = False+imbreak f x+  | f (head x) = True+  | otherwise = imbreak f (tail x)++--Boolean TRansform: (a,b) a if true, b if not, return the list.+--btr (==1) [1,2,3] ('a','b') -> "abb"+-- | Conditionally transform input based on the predicate. When it is true, fst of the tupple is used, snd otherwise.+btr :: (a -> Bool) -> [a] -> (b,b) -> [b]+btr _ [] _ = []+btr f (c:cs) (a,b)+  | f c = a : rest+  | otherwise = b : rest+  where rest = btr f cs (a,b)
+ Cookbook/Ingredients/Lists/Access.hs view
@@ -0,0 +1,26 @@+module Cookbook.Ingredients.Lists.Access(+count,contains,qsort) where++import qualified Cookbook.Ingredients.Functional.Break as Br++import qualified Cookbook.Common as Cm++--Count the number of occurances in a list.+-- | Counts the number of occurences within a list.+count :: (Eq a) => [a] -> a -> Int+count x c = sum $ Br.btr (==c) x (1,0)++-- | Checks to see if a greater list has a list within it.+contains :: (Eq a) => [a] -> [a] -> Bool+contains [] _ = False+contains x c+  | (take (length c) x) == c = True+  | otherwise = contains (tail x) c ++-- | Sorts a list from least to greatest. Compose with Cookbook.Ingredients.Lists.Modify.rev for greatest-to-least.+qsort :: (Ord a) => [a] -> [a]+qsort [] = []+qsort (x:xs) = lessT ++ [x] ++ greatT+  where+    lessT  = qsort [y | y <- xs, y <= x]+    greatT = qsort [y | y <- xs, y > x]
+ Cookbook/Ingredients/Lists/Modify.hs view
@@ -0,0 +1,24 @@+{-# LANGUAGE FlexibleContexts #-}+module Cookbook.Ingredients.Lists.Modify(+rev,rm,splitOn,snipe) where++import qualified Cookbook.Continuous as Cnt+import qualified Cookbook.Common as Com+--Reverse a list+--rev [1,2,3,4] -> [4,3,2,1]+-- | Reverses a list+rev :: [a] -> [a]+rev [] = []+rev (x:xs) = rev xs ++ [x]++-- | Removes all occurences from a list.+rm :: (Eq a) => [a] -> a -> [a]+rm x c = filter (/=c) x++-- | Create sub-lists based on a delimeter. The string 'joe,joe1,joe2' splitOn ',' would return a list of the three joes.+splitOn :: (Cnt.Continuous [a] a, 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] ++ (Com.sub x (c + 1))
+ Cookbook/Ingredients/Tupples/Look.hs view
@@ -0,0 +1,17 @@+module Cookbook.Ingredients.Tupples.Look(look,looklist,swp) where++-- | Returns the second element of the first tupple where the first element matches input.+look :: (Eq a) => [(a,b)] -> a -> (Maybe b)+look [] _ = Nothing+look ((a,b):bs) c+  | a == c = (Just b)+  | otherwise = look bs c++-- | Returns all second elements where (fst t) matches the input.+lookList :: (Eq a) => [(a,b)] -> a -> Maybe [b]+lookList d c = case filt of [] -> Nothing ; _ -> Just filt+  where filt = [b | (a,b) <- d, a == c]++-- | Swap the order of a second-degree tupple.+swp :: (a,b) -> (b,a)+swp (a,b) = (b,a)
+ Cookbook/Recipes/Configuration.hs view
@@ -0,0 +1,8 @@+module Cookbook.Recipes.Configuration(conf) where+import Cookbook.Ingredients.Lists.Access+import Cookbook.Ingredients.Lists.Modify+import Cookbook.Ingredients.Tupples.Look++conf :: [String] -> String -> String+conf x c = let configs = [let (d:f:_) = (splitOn y ':') in (d,f)| y <- x, (length y) > 2, ':' `elem` y] in case (look configs c) of (Just f) -> f+                                                                                                                                    (Nothing) -> []
+ Cookbook/Recipes/DiffStat.hs view
@@ -0,0 +1,46 @@+--Library for determining movement of data within a list. +--data Stat can have one of 3 forms:+--Less a = Back by a+--Even  = Same position+--Great a = Forward by a+module Cookbook.Recipes.DiffStat(Stat(..),stat,diff,patch) where++import Cookbook.Common+import Cookbook.Ingredients.Lists.Modify+import Cookbook.Ingredients.Lists.Access+import Cookbook.Ingredients.Tupples.Look++import Data.Maybe+data Stat a = Less a | Even |  Great a deriving (Show)++stat :: (Eq a) => [a] -> [a] -> a -> Stat Int+stat x c f+  | f `notElem` x = error "Unique entry found in stat x"+  | f `notElem` c = error "Unique entry found in stat c"+stat x c f+  | diff < 0  = Less diff+  | diff == 0 = Even+  | otherwise = Great diff+  where diff = (pos x f) - (pos c f)++diff :: (Eq a) => [a] -> [a] -> [Stat Int]+diff x [] = []+diff x (c:cs) = stat x (c:cs) c : diff x cs++--The compiler doesn't like this type signature for some reason.+--Here it is anyway, and :t in GHCI can even back it up:+--patch :: (Eq a) => [a] -> [Stat Int] -> [Int] +patch x c = assemble $ relatdiff posbind c+  where posbind = zip x [0..((length x))]+        +--Get the absolute positions of diff lists.+relatdiff :: (Eq a) => [(a,Int)] -> [Stat Int] -> [(a,Int)]+relatdiff [] _ = []+relatdiff _ [] = []+relatdiff ((a,b):bs) ((Great x):xs) = (a,(b + x)) : relatdiff bs xs+relatdiff ((a,b):bs) ((Less x):xs) = (a,(b - x)) : relatdiff bs xs+relatdiff ((a,b):bs) ((Even):xs) = (a,(b-1)) : relatdiff bs xs++--Assemble a relatdiff list+assemble :: [(a,Int)] -> [a]+assemble x = catMaybes $ map (look (map swp x)) $ qsort (map snd x)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Nate Pisarski++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Nate Pisarski nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ cookbook.cabal view
@@ -0,0 +1,57 @@+-- Initial cookbook.cabal generated by cabal init.  For further +-- documentation, see http://haskell.org/cabal/users-guide/++-- The name of the package.+name:                cookbook++-- The package version.  See the Haskell package versioning policy (PVP) +-- for standards guiding when and how versions should be incremented.+-- http://www.haskell.org/haskellwiki/Package_versioning_policy+-- PVP summary:      +-+------- breaking API changes+--                   | | +----- non-breaking API additions+--                   | | | +--- code changes with no API change+version:             0.1.0.0++-- A short (one-line) description of the package.+synopsis:            A silver-platter library in Haskell.++-- A longer description of the package.+-- description:         ++-- The license under which the package is released.+license:             BSD3++-- The file containing the license text.+license-file:        LICENSE++-- The package author(s).+author:              Nate Pisarski++-- An email address to which users can send suggestions, bug reports, and +-- patches.+maintainer:          nathanpisarski@gmail.com++--This package's github repository+--https://github.com/natepisarski/Cookbook-hs++-- A copyright notice.+-- copyright:           ++category:            Development++build-type:          Simple++-- Constraint on the version of Cabal needed to build this package.+cabal-version:       >=1.8+++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+  +  -- Modules included in this library but not exported.+  -- other-modules:       +  +  -- Other library packages from which modules are imported.+  build-depends:       base ==4.6.*+