cnd
cnd

Reputation: 33784

Seq.iter vs for - what difference?

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

Upvotes: 15

Views: 7741

Answers (5)

Tomas Petricek
Tomas Petricek

Reputation: 243126

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

Upvotes: 19

Ed'ka
Ed'ka

Reputation: 7158

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)

Upvotes: 0

Joh
Joh

Reputation: 2380

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.

Upvotes: 12

manojlds
manojlds

Reputation: 301527

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.

Upvotes: 1

Yin Zhu
Yin Zhu

Reputation: 17119

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.

Upvotes: 2

Related Questions