Last active
April 1, 2018 12:35
-
-
Save kevroletin/1daeca8cf8d35738c8e9ee07d536e6d9 to your computer and use it in GitHub Desktop.
Attempt to solve stream reordering problem with Streamly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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