获得输入到Netwire程序 [英] Getting input into Netwire programs

查看:123
本文介绍了获得输入到Netwire程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我没有问题写所有的电线,我想将输入转换为输出。

现在是编写IO包装以配合我的真实世界输入的时候了,我有点困惑。

我应该为 Wire semab s 参数创建一个自定义会话类型,并将我的传感器值嵌入如果是这样,我有这些问题:


  1. 这是怎么回事 class(Monoid s,Real t)=>的 Monoid s HasTime t s | s - >吨?它用于什么?

  2. 我正在考虑在传感器读数上加上 Map String Double ,但我的monoid紧缩字典?它应该是左偏吗?右侧偏置?以上都不是?

如果不是,我该怎么办?我想以形式的连线为一些 s 结束Wire的InhibitionReason Identity()Double ,表示我的输入。

这是我的理解,我不想或不需要使用 m 参数 Wire 用于此目的,从而允许电线本身是纯粹的,并将IO限制在逐步通过顶层线路的代码中。这是不正确的?

解决方案

将数据放入 Wire semab 通过输入 a 。可以通过使用 WPure WGen 来获取状态变量delta s 或底层 Monad m ,但这些让我们远离主要抽象。主要抽象为 Arrow Category ,它们只知道 ab ,而不是大约 sem



下面是一个非常简单的程序的例子,提供输入作为输入 A double 是程序的最外层。 repl 是一个小型的read-eval-print循环,它调用 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。 (constInhibited)id $ eb
repl w'

main = repl double

请注意,我们将时差传递给 stepWire ,而不是总时间。我们可以通过运行不同的顶级线程来检查这是否正确。

  timeString ::(HasTime ts ,Show t,Monad m)=> Wire是一个String 
timeString = arr节目。时间

main = repl timeString

具有所需的输出: p>

  a 
1
b
2
c
3


I'm getting started with Netwire version 5.

I have no problem writing all the wires I want to transform my inputs into my outputs.

Now the time has come to write the IO wrapper to tie in my real-world inputs, and I am a bit confused.

Am I supposed to create a custom session type for the s parameter of Wire s e m a b and embed my sensor values in there?

If so, I have these questions:

  1. What's up with the Monoid s context of class (Monoid s, Real t) => HasTime t s | s -> t? What is it used for?
  2. I was thinking of tacking on a Map String Double with my sensor readings, but how should my monoid crunch the dictionaries? Should it be left-biased? Right-biased? None of the above?

If not, what am I supposed to do? I want to end up with wires of the form Wire s InhibitionReason Identity () Double for some s, representing my input.

It's my understanding that I don't want or need to use the monadic m parameter of Wire for this purpose, allowing the wires themselves to be pure and confining the IO to the code that steps through the top-level wire(s). Is this incorrect?

解决方案

The simplest way to put data into a Wire s e m a b is via the input a. It's possible, through the use of WPure or WGen to get data out of the state delta s or the underlying Monad m, but these take us further away from the main abstractions. The main abstractions are Arrow and Category, which only know about a b, and not about s e m.

Here's an example of a very simple program, providing input as the input a. double is the outermost wire of the program. repl is a small read-eval-print loop that calls stepWire to run the wire.

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

Notice that we pass in the time difference to stepWire, not the total elapsed time. We can check that this is the correct thing to do by running a different top-level wire.

timeString :: (HasTime t s, Show t, Monad m) => Wire s e m a String
timeString = arr show . time

main = repl timeString 

Which has the desired output:

a
1
b
2
c
3

这篇关于获得输入到Netwire程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆