Printing Each Character Typed w/ Scalaz

。_饼干妹妹 提交于 2019-12-11 03:32:42

问题


I'm trying to write the following Haskell function, but in Scala (using scalaz):

ghci>let f = do { x <- getChar; _ <- putChar(x); return () }
ghci>f -- then type '4'
44ghci>

Here's what I tried:

def showInput: IO[Unit] = for {
    c      <- getChar
    _      <- putChar(c)
} yield ()

Then I ran it:

scala> net.repl.Foo.showInput
res2: scalaz.effect.IO[Unit] = scalaz.effect.IOFunctions$$anon$6@73e3ae3a

After running the method, I typed 1234. But, I had to press enter before the output (of 1) showed up:

scala> res2.unsafePerformIO
1 

How can I write the above scalaz function to behave identically to the Haskell function?

来源:https://stackoverflow.com/questions/29378195/printing-each-character-typed-w-scalaz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!