Dou*_*ean 11 haskell frp netwire
我开始使用Netwire第5版了.
编写我想要将输入转换为输出的所有电线都没有问题.
现在是编写IO包装器以配合我的实际输入的时候了,我有点困惑.
我是否应该为s
参数创建自定义会话类型Wire s e m a b
并将传感器值嵌入其中?
如果是这样,我有以下问题:
Monoid s
背景class (Monoid s, Real t) => HasTime t s | s -> t
?它是干什么用的?Map String Double
使用我的传感器读数,但是我的monoid应该如何处理字典呢?它应该留下偏见吗?右侧偏置?以上都不是?如果没有,我该怎么办?我想最终Wire s InhibitionReason Identity () Double
得到一些形式的电线s
,代表我的输入.
我的理解是,我不想或不需要为此目的使用monadic m
参数Wire
,允许电线本身是纯净的,并将IO限制在逐步通过顶层电线的代码中.这是不正确的?
将数据放入 a 的最简单方法Wire s e m a b
是通过 input a
。通过使用WPure
或从状态增量或底层WGen
中获取数据是可能的,但这些使我们远离主要抽象。主要的抽象是and ,它只知道关于,而不知道关于。s
Monad
m
Arrow
Category
a b
s e m
这是一个非常简单的程序示例,提供输入作为 input 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
Run Code Online (Sandbox Code Playgroud)
请注意,我们将时间差传递给stepWire
,而不是总经过时间。我们可以通过运行不同的顶级线路来检查这是否正确。
timeString :: (HasTime t s, Show t, Monad m) => Wire s e m a String
timeString = arr show . time
main = repl timeString
Run Code Online (Sandbox Code Playgroud)
其具有所需的输出:
a
1
b
2
c
3
Run Code Online (Sandbox Code Playgroud)