f# - 如何将 F# 中的模块导入限制在本地范围内?

标签 f# namespaces module scope

是否可以在本地限制模块的导入,最好将其与 Module Abbreviations 结合使用?目标是避免使用来自导入的符号污染我当前的模块。

例如(受 OCaml 启发)类似的东西:

let numOfEvenIntegersSquaredGreaterThan n =
    let module A = Microsoft.FSharp.Collections.Array in
        [|1..100|] |> A.filter (fun x -> x % 2 = 0)
                   |> A.map    (fun x -> x * x)
                   |> A.filter (fun x -> x > n)
                   |> A.length

let elementsGreaterThan n =
    let module A = Microsoft.FSharp.Collections.List in
        [1..100] |> A.filter (fun x -> x > n)

另外有没有办法用命名空间实现类似的东西?

最佳答案

The goal is to avoid polluting my current module with symbols from imports.



请注意 open Array在 F# 中是不允许的(与 OCaml 相反)。
您可以在模块上使用缩写,但只能在全局范围内:
module A = Microsoft.FSharp.Collections.Array

您可以使用 Array 代替 Microsoft.FSharp.Collections.Array。所以你的代码是:
let numOfEvenIntegersSquaredGreaterThan n =
    [|1..100|] |> Array.filter (fun x -> x % 2 = 0)
               |> Array.map    (fun x -> x * x)
               |> Array.filter (fun x -> x > n)
               |> Array.length

如果您想对数组和列表重复使用相同的代码,您可能需要使用 Seq模块:
let elementsGreaterThan n =
    [1..100] |> Seq.filter (fun x -> x > n)

关于f# - 如何将 F# 中的模块导入限制在本地范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5633672/

相关文章:

f# - 如何在不使用 upcast 的情况下编写 (string*obj) 列表

f# - 如何在 F# 中对 LIST 进行排序

javascript - 关于 Nodejs 模块的错误

python - 如何使用加密模块加密变量?

c++ - 从其他进程获取模块句柄

c# - 请确认或更正我的这个 Haskell 代码片段的 "English interpretation"

f# - 在一个元组序列中,我能找到一个给定标准的元组吗?

namespaces - Arduino如何使用命名空间?

c++ - 我正在编写一个 cpp 程序来打印两个数字之间的所有质数。程序运行成功但未打印任何内容

C++:使用传入的字符串参数访问类型中的内容