F #에서 시퀀스를 사용하여 계산을 강제로 강제로 강제하는 가장 관용적 인 스타일은 무엇입니까?
문제
나는 평등하게 효과적인 조작
securities |> Seq.map (fun x -> request.Append("securities",x))
.
코드를 수행하는 가장 관용적 인 방법은 무엇입니까?
나는 seq.doit을 썼지 만, 가려움증
module Seq =
let Doit sa = sa |> Seq.toArray |> ignore
. 해결책
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
다른 팁
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))
제휴하지 않습니다 StackOverflow