Skip to content

Instantly share code, notes, and snippets.

@kevroletin
Last active April 1, 2018 12:35
Show Gist options
  • Save kevroletin/1daeca8cf8d35738c8e9ee07d536e6d9 to your computer and use it in GitHub Desktop.
Save kevroletin/1daeca8cf8d35738c8e9ee07d536e6d9 to your computer and use it in GitHub Desktop.
Attempt to solve stream reordering problem with Streamly
{-# LANGUAGE FlexibleContexts #-}
module Main where
import Data.Monoid
import qualified Streamly as S
import qualified Streamly.Prelude as S
-- Wrong: this should be
-- Int -> S.StreamT m a -> S.StreamT m a
reorderStreamly :: Monad m
=> Int -> S.StreamT m a -> S.StreamT m [a]
reorderStreamly n stream =
S.scan accum (mempty, []) fst
$ fmap Just stream
<> pure Nothing
where
accum (_, buff) (Just x)
| length buff == n - 1 = (x : buff, [])
| otherwise = (mempty, x : buff)
accum (_, buff) Nothing = (buff, [])
streamlyDemo :: Monad m => m [Int]
streamlyDemo = S.foldl (<>) [] id $ reorderStreamly 3 (S.each [1 .. 10])
main :: IO ()
main = do print $ streamlyDemo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment