Search code examples
arraysf#sequencesdeferred

What is the most idiomatic style to force a computation using Sequences in f#?


I have a side-effective operation

     securities |> Seq.map (fun x -> request.Append("securities",x))

What is the most idiomatic way to have the code perform ?

I wrote a Seq.Doit, but it itches

  module Seq =
     let Doit sa = sa |> Seq.toArray |> ignore

Solution

  • Deferred sequences are used when you create sequences using Seq.delay or sequence expression seq{}. Any function on sequence returning any datatype other than seq can force computation.

    Alternatively, you can use for loop instead of Seq.iter:

    for s in securities do
       request.Append("securities", s)
    

    If you want to hide side effects and return request for later use, Seq.fold is a good choice:

    securities |> Seq.fold (fun acc x -> acc.Append("securities", x); acc) request