Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F#使用定时器接收_F#_System.reactive - Fatal编程技术网

F#使用定时器接收

F#使用定时器接收,f#,system.reactive,F#,System.reactive,我正在学习RX,并尝试将一些代码从C移植到F。 下面是使用计时器的C#示例: Console.WriteLine(“当前时间:+DateTime.Now”); var source=Observable.Timer(TimeSpan.FromSeconds(5),TimeSpan.FromSeconds(1)).Timestamp(); 使用(source.Subscribe(x=>Console.WriteLine(“{0}:{1}”,x.Value,x.Timestamp))) { Cons

我正在学习RX,并尝试将一些代码从C移植到F。 下面是使用计时器的C#示例:

Console.WriteLine(“当前时间:+DateTime.Now”);
var source=Observable.Timer(TimeSpan.FromSeconds(5),TimeSpan.FromSeconds(1)).Timestamp();
使用(source.Subscribe(x=>Console.WriteLine(“{0}:{1}”,x.Value,x.Timestamp)))
{
Console.WriteLine(“按任意键取消订阅”);
Console.ReadKey();
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
以下是我尝试执行相同操作的代码:

#light
open System
open System.Collections.Generic
open System.Linq
open System.Reactive
open System.Reactive.Linq
open System.Reactive.Subjects


printfn "Current Time: %A" DateTime.Now

let source = Observable.Timer(TimeSpan.FromSeconds(5.0), TimeSpan.FromSeconds(1.0)).Timestamp()
source.Subscribe(fun x -> printfn "%A %A" x.Value x.Timestamp) |> ignore
但我有编译器错误:

错误1基于此程序点之前的信息查找不确定类型的对象。在此程序点之前可能需要类型注释来约束对象的类型。这样可以解决查找问题

我不知道
x.Value
x.Timestamp
的类型是什么。 顺便说一句,我也不知道如何用C语言在F语言中重写。 请告诉我正确的代码。

下面是从C到F的“直接翻译”:

开放系统
开放系统,反应式
开放系统.Reactive.Linq
打印fn“当前时间:%A”日期时间。现在
让source=Observable.Timer(TimeSpan.FromSeconds(5.0),TimeSpan.FromSeconds(1.0)).Timestamp()
使用(source.Subscribe(fun(x:Timestamp)->printfn“%A%A”x.Value x.Timestamp))
(乐趣->
printfn“按任意键取消订阅”
Console.ReadKey()|>忽略
)
printfn“按任意键停止”
Console.ReadKey()|>忽略
运行时,允许它在看到1秒计时器事件如何开始流入之前经过5秒

附录:lambda表达式中的输入参数类型,它依次是
Iobservable的参数。Subscribe()
是我们称之为
Subscribe()
Iobservable的值类型,即构成
Iobservable
源的值类型

反过来,
source
表示
Observable.Timer(DateTimeOffset,TimeSpan)
方法的结果,该方法返回一个可观察序列,该序列在适当的时间生成一个值,然后在每个周期之后生成。此序列具有类型
IObservable

Timestamp()


因此,最终我们的
IObservable
类型的
时间戳
,上面的代码片段反映为
Subscribe()

Subscribe()中匿名函数的显式参数
x
,因为它过载而无法工作。如果指定要使用的重载,则该重载有效

(source.Subscribe : (Timestamped<int64> -> unit) -> IDisposable)(fun x -> printfn "%A %A" x.Value x.Timestamp) |> ignore

嗨,吉恩·贝利茨基:非常感谢,你的代码很有效!但是我很难知道x的类型,因为在C#代码中,x的类型也丢失了。@约翰:由于对所涉及类型的解释比max允许的注释占用更多的空间,我将其作为原始答案的补充。为清楚起见,您可能希望自己跟踪此类型转换链,并将其用作参考。
(source.Subscribe : (Timestamped<int64> -> unit) -> IDisposable)(fun x -> printfn "%A %A" x.Value x.Timestamp) |> ignore
printfn "Current Time: %A" DateTime.Now

let source = Observable.Timer(TimeSpan.FromSeconds(5.0), TimeSpan.FromSeconds(1.0)).Timestamp()
source.Add(fun x -> printfn "%A %A" x.Value x.Timestamp)

printfn "Press any key to stop"
Console.ReadKey() |> ignore