Domanda

Ho un'operazione fianco-efficace

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

Qual è il modo più idiomatico per eseguire il codice?

Ho scritto un seq.doit, ma prurito

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

È stato utile?

Soluzione

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

Altri suggerimenti

I think Seq.iter is appropriate in this case. From the MSDN reference page:

Seq.iter : ('T -> unit) -> seq<'T> -> unit

Applies the given function to each element of the collection.

So, supposing that request.Append does not return anything, your code becomes:

securities |> Seq.iter (fun x -> request.Append("securities", x))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top