我可以
for event in linq.Deltas do
Run Code Online (Sandbox Code Playgroud)
或者我能做到
linq.Deltas |> Seq.iter(fun event ->
Run Code Online (Sandbox Code Playgroud)
所以我不确定这是否相同.如果那不一样,我想知道区别.我不能选择使用什么:iter或for.
添加 - 所以,如果这是选择的问题,我更喜欢iter在顶级使用并且for用于clousures
增加了一些后来 -看起来像iterIS map+ ignore-这是从使用势在必行忽略字的运行方式.所以它看起来像功能方式......
Tom*_*cek 17
正如其他人提到的,存在一些差异(iter支持非泛型IEnumerator,你可以改变mutable值for).这些有时是重要的差异,但大多数时候你可以自由选择使用哪一个.
我通常更喜欢for(如果有语言结构,为什么不使用它?).iter看起来更好的情况是当你有一个需要调用的函数时(例如使用部分应用程序):
// I would write this:
strings |> Seq.iter (printfn "%c")
// instead of:
for s in strings do printfn "%c" s
Run Code Online (Sandbox Code Playgroud)
同样,iter如果在某些处理管道的末尾使用它,则使用更好:
// I would write this:
inputs |> Seq.filter (fun x -> x > 0)
|> Seq.iter (fun x -> foo x)
// instead of:
let filtered = inputs |> Seq.filter (fun x -> x > 0)
for x in filtered do foo x
Run Code Online (Sandbox Code Playgroud)
Joh*_*Joh 10
您可以从for循环体修改可变变量.你不能通过闭包来做到这一点,这意味着你无法使用它iter.(注意:我在谈论在for/ 之外声明的iter可变变量.可以访问本地可变变量.)
考虑到要点iter是执行一些副作用,差异可能很重要.
我个人很少使用iter,因为我发现for更清楚.