module Sublists where
import Control.Applicative (Applicative,pure,(<*>))
import Data.Functor ((<$>))
intoRegularSublists :: [Int] -> [a] -> [[a]]
intoRegularSublists :: [Int] -> [a] -> [[a]]
intoRegularSublists (Int
n:[Int]
ns) [a]
xs
| [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
xs = []
| Bool
otherwise = [a]
ts [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [Int] -> [a] -> [Int] -> [[a]]
forall a. [Int] -> [a] -> [Int] -> [[a]]
intoRegularSublists' [Int]
ns [a]
zs (Int
nInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:[Int]
ns)
where ([a]
ts, [a]
zs) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [a]
xs
intoRegularSublists' :: [Int] -> [a] -> [Int] -> [[a]]
intoRegularSublists' (Int
r:[Int]
rs) [a]
ys [Int]
us
| [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [a]
ys = []
| Bool
otherwise = [a]
ws [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [Int] -> [a] -> [Int] -> [[a]]
intoRegularSublists' [Int]
rs [a]
vs [Int]
us
where ([a]
ws,[a]
vs) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
r [a]
ys
intoRegularSublists' [Int]
_ [a]
ys [Int]
us = [Int] -> [a] -> [Int] -> [[a]]
intoRegularSublists' [Int]
us [a]
ys [Int]
us
intoRegularSublists [Int]
_ [a]
xs = [[a]
xs]
{-# INLINABLE intoRegularSublists #-}
intoRegularSublistsM :: (Monad m)
=> (a -> m [Int])
-> a
-> [b]
-> m [[b]]
intoRegularSublistsM :: (a -> m [Int]) -> a -> [b] -> m [[b]]
intoRegularSublistsM a -> m [Int]
f a
seed [b]
xs
| [b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [b]
xs = [[b]] -> m [[b]]
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise = a -> m [Int]
f a
seed m [Int] -> ([Int] -> m [[b]]) -> m [[b]]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \[Int]
ns -> [[b]] -> m [[b]]
forall (m :: * -> *) a. Monad m => a -> m a
return ([[b]] -> m [[b]]) -> ([b] -> [[b]]) -> [b] -> m [[b]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> [b] -> [[b]]
forall a. [Int] -> [a] -> [[a]]
intoRegularSublists [Int]
ns ([b] -> m [[b]]) -> [b] -> m [[b]]
forall a b. (a -> b) -> a -> b
$ [b]
xs
{-# INLINABLE intoRegularSublistsM #-}
intoRegularSublistsA :: (Applicative f)
=> (a -> f [Int])
-> a
-> [b]
-> f [[b]]
intoRegularSublistsA :: (a -> f [Int]) -> a -> [b] -> f [[b]]
intoRegularSublistsA a -> f [Int]
f a
seed [b]
xs
| [b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [b]
xs = [[b]] -> f [[b]]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
| Bool
otherwise = (\[Int]
ns [b]
xs -> [Int] -> [b] -> [[b]]
forall a. [Int] -> [a] -> [[a]]
intoRegularSublists [Int]
ns [b]
xs) ([Int] -> [b] -> [[b]]) -> f [Int] -> f ([b] -> [[b]])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f [Int]
f a
seed f ([b] -> [[b]]) -> f [b] -> f [[b]]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [b] -> f [b]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [b]
xs
{-# INLINABLE intoRegularSublistsA #-}