0

As per the streamly tutorial, it is possible to combine different streams using <> operator (re-exported from Semigroup by streamly) like this:

runStream $ ((readLn :: IO Int) |: nil) <> ((readLn :: IO Int) |: nil) & S.mapM print

However, I'd like to combine streams which have different types, but both work with print. Something like this:

runStream $ ((readLn :: IO Int) |: nil) <> ((readLn :: IO [Char]) |: nil) & S.mapM print

But this gives me an error:

<interactive>:27:45: error:
    • Couldn't match type ‘[Char]’ with ‘Int’
      Expected type: SerialT IO Int
        Actual type: SerialT IO [Char]
    • In the second argument of ‘(<>)’, namely
        ‘((readLn :: IO [Char]) |: nil)’
      In the first argument of ‘(&)’, namely
        ‘((readLn :: IO Int) |: nil) <> ((readLn :: IO [Char]) |: nil)’
      In the second argument of ‘($)’, namely
        ‘((readLn :: IO Int) |: nil) <> ((readLn :: IO [Char]) |: nil)
           & S.mapM print’

Any hints on how to do this?


The above code has been run in ghci with these are the imports:

import Streamly
import Streamly.Prelude ((|:), nil)
import qualified Streamly.Prelude as S
import Data.Function ((&))
zoran119
  • 10,657
  • 12
  • 46
  • 88

1 Answers1

1

Since S.mapM only works on things that are already monads, presumably you can convert them to a shared type -- say, ones containing Strings -- then traverse the shared stream type. print is just putStrLn . show, so:

runStream $ (show <$> ((readLn :: IO Int) |: nil)) <> (show <$> ((readLn :: IO [Char]) |: nil)) & S.mapM putStrLn
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • That seems to work! Say the first stream had lots of elements, any idea if `show <$>` would impact the 'streaminess' of it (ie. would it start collecting results memory for some strange reason)? – zoran119 May 25 '18 at 14:12
  • @zoran119 It seems unlikely to me that `show <$> stream` would have significantly worse "streaminess" than `stream`, but take that with a grain of salt, since I have never heard of the streamly library before approximately 40 minutes ago. – Daniel Wagner May 25 '18 at 14:46