根据streamly tutorial,可以使用<>
运算符(Semigroup
streamly
runStream $ ((readLn :: IO Int) |: nil) <> ((readLn :: IO Int) |: nil) & S.mapM print
)组合不同的流,如下所示:
print
但是,我想要组合具有不同类型的流,但两者都适用于runStream $ ((readLn :: IO Int) |: nil) <> ((readLn :: IO [Char]) |: nil) & S.mapM print
。像这样:
<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’
但这给了我一个错误:
ghci
有关如何执行此操作的任何提示?
以上代码已在import Streamly
import Streamly.Prelude ((|:), nil)
import qualified Streamly.Prelude as S
import Data.Function ((&))
中运行,其中包含导入:
From|To |Amt |In_out
A | B | 100 |0
B | A | 200 |1
C | D | 250 |0
答案 0 :(得分:1)
由于S.mapM
仅适用于已经是monad的东西,可能你可以将它们转换为共享类型 - 比如包含String
s的那些 - 然后遍历共享流类型。 print
只是putStrLn . show
,所以:
runStream $ (show <$> ((readLn :: IO Int) |: nil)) <> (show <$> ((readLn :: IO [Char]) |: nil)) & S.mapM putStrLn