module Cookbook.Ingredients.Lists.Encompass where
import qualified Cookbook.Essential.Continuous as Ct
import qualified Cookbook.Essential.Common as Cm
import qualified Cookbook.Ingredients.Functional.Break as Br
encompassing :: (Eq a) => [a] -> (a,a) -> [a]
encompassing (x:xs) (a,b) = helper (x:xs) 0
where
helper [] _ = []
helper (y:ys) e
| y == b && e <= 1 = []
| y == a && a == b = if e == 0 then [] else condInp $ helper ys (e + 1)
| y == a = condInp $ helper ys (e + 1)
| y == b = y : helper ys (e 1)
| otherwise = condInp $ helper ys e
where
condInp = ([y | e > 0] ++)
afterEncompassing :: (Eq a) => [a] -> (a,a) -> [a]
afterEncompassing a (b,c) = tail $ Ct.after a $ encompassing a (b,c)
beforeEncompassing :: (Eq a) => [a] -> (a,a) -> [a]
beforeEncompassing a (b,c) = let temp = Ct.before a $ encompassing a (b,c) in take (length temp 1) temp
splitEncompassing :: (Eq a) => [a] -> (a,a) -> [[a]]
splitEncompassing a (b,c) = filter (\x -> x /= []) (helper a (b,c))
where
helper e (f,g)
| not $ f `elem` e && g `elem` e = [e]
| otherwise = beforeEncompassing e (f,g) : splitEncompassing (afterEncompassing e (f,g)) (f,g)
notEncompassedSplit :: (Eq a) => [a] -> (a,a) -> a -> [[a]]
notEncompassedSplit [] _ _ = []
notEncompassedSplit ls (sp1,sp2) sp = toGaps $ helper ls 0
where
helper [] _ = []
helper (x:xs) c
| x == sp1 && c >= 0 = (sp1: encompassing (x:xs) (sp1,sp2)) : helper (afterEncompassing xs (sp1,sp2)) (c + 1)
| x == sp2 && c <= 0 = [x] : helper xs (c 1)
| x == sp1 = [x] : helper xs (c+1)
| x == sp2 = [x] : helper xs (c 1)
| x == sp && c > 0 = [x] : helper xs c
| x == sp && c <= 0 = [] : helper xs c
| otherwise = [x] : helper xs c
toGaps :: (Eq a) => [[a]] -> [[a]]
toGaps [] = []
toGaps x
| [] `notElem` x = [Cm.flt x]
| otherwise = (Cm.flt (Br.filterBreak (\c -> c /= []) x)) : (toGaps $ tail (Br.removeBreak (\c -> c /= []) x))
gatherEncompassing :: (Eq a) => [a] -> (a,a) -> [[a]]
gatherEncompassing a (b,c)
| not $ b `elem` a && c `elem` a = []
| otherwise = encompassing a (b,c) : gatherEncompassing (afterEncompassing a (b,c)) (b,c)