我希望下面第一行中的引号能够在F#中急切地评估。它被懒惰地评估了两次。为什么呢?
let quotes = getFundsClosingPrice dbFunds // httpGet the closing prices
quotes
|> fun quotes ->
let maxDate =
quotes // <- quotes evaluated 1st time
|> Seq.maxBy (
fun (quote) ->
quote.TradedOn)
|> fun q ->
q.TradedOn
quotes
|> Seq.map
(fun (quote) -> // <- quotes evaluated 2nd time. Why??
{
Symbol = quote.Symbol;
ClosingPrice = quote.ClosingPrice;
TradedOn = maxDate
}
)
我如何热切地评估它?
答案 0 :(得分:7)
Seq是IEnumerable,提供大量便捷功能。每个地图功能(和相关的)从一开始就评估序列。
您可以在开头将序列转换为列表或数组:
let quotes = getFundsClosingPrice dbFunds |> List.ofSeq
或者您可以使用Seq.cache