我在期待 报价 在下面的第一行中,要在 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
}
)
我怎么能急切地评价它?
最佳答案
Seq 是 IEnumerable,具有大量方便的功能。每个映射函数(和相关函数)从一开始就评估序列。
您可以在开头将序列转换为列表或数组:
let quotes = getFundsClosingPrice dbFunds |> List.ofSeq
或者您可以使用 Seq.cache
关于f# - 为什么 F# 懒惰地评估这个,并且在下面两次评估?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813524/