缓存函数结果 f#

标签 caching f#

我有一个函数,它的参数是常数,例如

let is_prime x = (test)

但它非常大而且速度很慢。所以我希望它的结果只计算一次,而我可以随时调用它。

我试图以一种非函数式语言的方式做到这一点:
let _is_prime x = (test)

let mutable _is_prime_primes = []
let mutable _is_prime_tested = []

let is_prime x =
    if List.exists (fun el -> el = x) _is_prime_primes then
        true
    else
        if List.exists (fun el -> el = x) _is_prime_tested then
        false
    else 
        let result = _is_prime x
        if result then _is_prime_primes <- x :: _is_prime_primes
        _is_prime_tested <- x :: _is_prime_tested
        result

但我认为我大错特错。缓存这样的结果对于函数式语言来说必须是非常常见和简单的事情。

最佳答案

这是Internet Archive关联。

关于缓存函数结果 f#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1976705/

相关文章:

caching - 如何以编程方式清除 Drupal 7 中缓存的 View ?

caching - RESULT_CACHE RELIES_ON (NLS_SESSION_PARAMETERS)

.net - 如何将函数调用嵌入到另一个函数中?

F# 类型推断遗漏给定信息

f# - 如何有效地模式匹配?

f# - 如何将真正是 int32[] 的 System.Object 转换为 double[]?

arrays - F# 将字符串添加到字符串数组中

Hibernate 缓存级别 1

android - 在 Parse 中,我可以强制获取相关的 ParseObjects 吗?

php - MySQL查询缓存与应用层缓存结果集