f# - 为什么 F# 懒惰地评估这个,并且在下面两次评估?

标签 f# lazy-evaluation

我在期待 报价 在下面的第一行中,要在 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/

相关文章:

c++ - 具有中间变量的 Eigen 惰性评估

haskell - 理解 Haskell 的惰性

c# - 如何使树节点的值成为 IEnumerable?

f# - 如何避免基于长模式匹配的函数?

compiler-construction - F#中的属性语法系统

f# - F# 语言引用文档是否以离线格式(PDF、CHM)提供?

haskell - Haskell 中的 "Lazy IO"?

f# - 我可以将参数传递给 F# FAKE 构建脚本吗?

.net - F#记录与.net结构

Haskell,测量函数的 CPU 时间