Skip to content

Instantly share code, notes, and snippets.

@sarahzrf
Created September 1, 2021 03:50
Show Gist options
  • Save sarahzrf/8956fdbed7134bc5e26df66f1ee88510 to your computer and use it in GitHub Desktop.
Save sarahzrf/8956fdbed7134bc5e26df66f1ee88510 to your computer and use it in GitHub Desktop.
module Collated where
data CQueue a = CQ [a] [a] deriving (Show)
interleave :: [a] -> [a] -> [a]
interleave [] _ = []
interleave (x:xs) ys = x : interleave ys xs
buildCQ :: [a] -> CQueue a
buildCQ l = CQ (interleave l1 (reverse l2)) []
where n = length l `div` 2
(l1, l2) = splitAt n l
process1 :: Functor f => (a -> f a) -> CQueue a -> f (CQueue a)
process1 f (CQ [] ys) = process1 f (CQ ys [])
process1 f (CQ (x:x':xs) ys) = fmap go (f x)
where go y = CQ xs (x':y:ys)
exampleStep :: Int -> IO Int
exampleStep n = print n >> return (n + 10)
demo :: Int -> CQueue Int -> IO ()
demo 0 _ = return ()
demo n cq = do
cq' <- process1 exampleStep cq
demo (n - 1) cq'
main :: IO ()
main = demo 30 (buildCQ [0..9])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment