Seq.iter vs for-有什么区别? [英] Seq.iter vs for - what difference?
问题描述
我可以做到
for event in linq.Deltas do
或者我可以做
linq.Deltas |> Seq.iter(fun event ->
所以我不确定是否相同.如果那不一样,我想知道区别.我不知道该使用什么:iter
或for
.
So I'm not sure if that is the same. If that is not the same I want to know the difference. I don't know what to use: iter
or for
.
已添加-因此,如果这是选择的问题,那么我更愿意在顶层使用iter
,而for
用于闭包
added - so if that is the matter of choice I prefer to use iter
on a top level and for
is for closures
稍后再添加-看起来iter
是map
+ ignore
-这是使用命令式忽略词的一种方式.所以看起来就像是功能方式...
added some later - looking like iter
is map
+ ignore
- it's the way to run from using imperative ignore word. So it's looking like functional way ...
推荐答案
您可以从for
循环的主体中修改可变变量.您不能从闭包中做到这一点,这意味着您不能使用iter
做到这一点. (注意:我说的是在for
/iter
外部声明的可变变量.可以访问局部可变变量.)
You can modify mutable variables from the body of a for
loop. You can't do that from a closure, which implies you can't do that using iter
. (Note: I'm talking about mutable variables declared outside of the for
/ iter
. Local mutable variables are accessible.)
考虑到iter
的作用是要产生一些副作用,所以区别可能很重要.
Considering that the point of iter
is to perform some side effect, the difference can be important.
我个人很少使用iter
,因为我发现for
更清楚.
I personally seldom use iter
, as I find for
to be clearer.
这篇关于Seq.iter vs for-有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!