如何将F#中模块的导入限制为本地范围?
是否可以本地限制模块的导入,最好将其与如何将F#中模块的导入限制为本地范围?,f#,namespaces,module,scope,F#,Namespaces,Module,Scope,是否可以本地限制模块的导入,最好将其与模块缩写结合使用?目标是避免导入的符号污染我当前的模块 e、 g.(受OCaml启发)类似于: let numOfEvenIntegersSquaredGreaterThan n = let module A = Microsoft.FSharp.Collections.Array in [|1..100|] |> A.filter (fun x -> x % 2 = 0) |>
模块缩写结合使用
?目标是避免导入的符号污染我当前的模块
e、 g.(受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)
此外,是否有一种方法可以实现与名称空间类似的功能
目的是避免污染我的身体
带有来自的符号的当前模块
进口
请注意,F#中不允许使用开放数组
(与OCaml相反)。
您可以在模块上使用缩写,但只能在全局范围内使用:
module A = Microsoft.FSharp.Collections.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)
谢谢你的回答,劳伦特。不过,使用数组只是一个随意的例子,回想起来很不幸。我的目标是只在有限的范围内打开任意模块(或命名空间),就像在单个“let绑定”中一样。如果我理解正确的话,这(目前?)在F#中确实是不可能的。我希望这在将来是可能的。同时,我认为在全局范围内添加一些缩写不会造成太大的污染(但尽量避免使用1字符名称)。