Cay
Cay

Reputation:

How do I get the current sequence number in an iteration in F#?

Consider the following code to demonstrate the question:

let sequence = Seq.initInfinite (fun _ -> "Element")
Seq.iter (fun _ -> printf "Element no: ?") sequence 

Is it in any way possible to get the current sequence number (e.g. its rank) to print?

Upvotes: 3

Views: 363

Answers (1)

cfern
cfern

Reputation: 6006

Use the iteri function:

let sequence = Seq.initInfinite (fun _ -> "Element")
sequence |> Seq.iteri (fun i _ -> printfn "Element no. %d" i) 

Upvotes: 7

Related Questions