module Data.List.Filter (
takeEvery
, dropEvery
, takeUntil
, dropUntil
) where
takeEvery :: Int -> [a] -> [a]
takeEvery :: Int -> [a] -> [a]
takeEvery step :: Int
step xs :: [a]
xs = [a] -> [a]
forall a. [a] -> [a]
compute [a]
validated
where
compute :: [a] -> [a]
compute ys :: [a]
ys = case Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop (Int
step Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) [a]
ys of
[] -> []
y :: a
y:ys' :: [a]
ys' -> a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a]
compute [a]
ys'
validated :: [a]
validated
| Int
step Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = [a]
xs
| Bool
otherwise = [Char] -> [a]
forall a. HasCallStack => [Char] -> a
error ([Char] -> [a]) -> [Char] -> [a]
forall a b. (a -> b) -> a -> b
$ "Data.List.Transform.takeEvery: Step parameter "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ "must be positive."
dropEvery :: Int -> [a] -> [a]
dropEvery :: Int -> [a] -> [a]
dropEvery step :: Int
step xs :: [a]
xs = [a] -> [a]
forall a. [a] -> [a]
compute [a]
validated
where
compute :: [a] -> [a]
compute ys :: [a]
ys = case Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt (Int
step Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) [a]
ys of
(as :: [a]
as, []) -> [a]
as
(as :: [a]
as, _:bs :: [a]
bs) -> [a]
as [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a] -> [a]
compute [a]
bs
validated :: [a]
validated
| Int
step Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 = [a]
xs
| Bool
otherwise = [Char] -> [a]
forall a. HasCallStack => [Char] -> a
error ([Char] -> [a]) -> [Char] -> [a]
forall a b. (a -> b) -> a -> b
$ "Data.List.Transform.dropEvery: Step parameter "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ "must be positive."
takeUntil :: (a -> Bool) -> [a] -> [a]
takeUntil :: (a -> Bool) -> [a] -> [a]
takeUntil _ [] = []
takeUntil _ [x :: a
x] = [a
x]
takeUntil f :: a -> Bool
f (x :: a
x:xs :: [a]
xs) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (if a -> Bool
f a
x then [] else (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
takeUntil a -> Bool
f [a]
xs)
dropUntil :: (a -> Bool) -> [a] -> [a]
dropUntil :: (a -> Bool) -> [a] -> [a]
dropUntil _ [] = []
dropUntil f :: a -> Bool
f (x :: a
x:xs :: [a]
xs) = if a -> Bool
f a
x then a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs else (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropUntil a -> Bool
f [a]
xs