Seq.iter vs for - what difference?

纵饮孤独 提交于 2019-12-21 03:57:28

问题


I can do

for event in linq.Deltas do

or I can do

linq.Deltas |> Seq.iter(fun event ->

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.

added - so if that is the matter of choice I prefer to use iter on a top level and for is for closures

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 ...


回答1:


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.)

Considering that the point of iter is to perform some side effect, the difference can be important.

I personally seldom use iter, as I find for to be clearer.




回答2:


As others mentioned, there are some differences (iter supports non-generic IEnumerator and you can mutate mutable values in for). These are sometimes important differences, but most of the times you can freely choose which one to use.

I generally prefer for (if there is a language construct, why not use it?). The cases where iter looks nicer are when you have a function that you need to call (e.g. using partial application):

// I would write this:
strings |> Seq.iter (printfn "%c")

// instead of:
for s in strings do printfn "%c" s

Similarly, using iter is nicer if you use it at the end of some processing pipeline:

// 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



回答3:


For most of the situations, they are the same. I would prefer the first use. It looks clear to me.

The difference is that for in loop support IEnumerable objects, while Seq.iter requires that your collection (linq.deltas) is IEnumerable<T>.

E.g. MatchCollection class in .net regular expression inherits IEnumerable not IEnumerable<T>, you cannot use Seq.map or Seq.iter directly on it. But you can use for in loop.




回答4:


It is the style of programming. Imperative vs using functional programming. Keep in mind that F# is not a pure functional programming language.

Generally, use Seq.Iter if it is a part of some large pipeline processing, as that makes it much more clearer, but for ordinary case I think the imperative way is clearer. Sometime it is a personal preference, sometimes it is other issues like performance.




回答5:


for in F# is a form of list comprehension - bread and butter of functional programming while Seq.iter is a 'for side-effects only' imperative construct - not a sign of a functional code. Here what you can do with for:

let pairsTo n = seq {
    for i in [1..n] do
        for j in [i..n] do
             if j%i <> 0 then yield (i,j) }

printf "%A" (pairsTo 10 |> Seq.toList)


来源:https://stackoverflow.com/questions/5726566/seq-iter-vs-for-what-difference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!