我开始使用 Netwire 版本 5。
我可以毫无问题地编写我想将输入转换为输出的所有线路。
现在是时候编写 IO 包装器来绑定(bind)我的实际输入了,我有点困惑。
我应该为 s
创建自定义 session 类型吗? Wire s e m a b
的参数并将我的传感器值嵌入其中?
如果是这样,我有这些问题:
Monoid s
怎么了class (Monoid s, Real t) => HasTime t s | s -> t
的上下文?它是干什么用的? Map String Double
用我的传感器读数,但我的幺半群应该如何处理字典?应该偏左吗?偏右?以上都不是? 如果没有,我该怎么办?我想以
Wire s InhibitionReason Identity () Double
形式的电线结束对于一些 s
,代表我的输入。我的理解是我不想或不需要使用单子(monad)
m
Wire
的参数为此,允许线路本身是纯的,并将 IO 限制在通过顶层线路的代码中。这是不正确的吗?
最佳答案
将数据放入 Wire s e m a b
的最简单方法是通过输入 a
.这是可能的,通过使用 WPure
或 WGen
从状态 delta s
中获取数据或基础 Monad
m
,但是这些使我们远离主要抽象。主要的抽象是Arrow
和 Category
,只知道a b
,而不是关于 s e m
.
这是一个非常简单的程序示例,提供输入作为输入 a
. double
是程序的最外层线。 repl
是一个调用 stepWire
的小型读取-评估-打印循环运行电线。
import FRP.Netwire
import Control.Wire.Core
import Prelude hiding (id, (.))
double :: Arrow a => a [x] [x]
double = arr (\xs -> xs ++ xs)
repl :: Wire (Timed Int ()) e IO String String -> IO ()
repl w = do
a <- getLine
(eb, w') <- stepWire w (Timed 1 ()) (Right a)
putStrLn . either (const "Inhibited") id $ eb
repl w'
main = repl double
请注意,我们将时间差传递给
stepWire
,而不是总耗时。我们可以通过运行不同的顶层线来检查这是否正确。timeString :: (HasTime t s, Show t, Monad m) => Wire s e m a String
timeString = arr show . time
main = repl timeString
哪个具有所需的输出:
a
1
b
2
c
3
关于haskell - 获取 Netwire 程序的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851565/