是否可以在F#模块中使用私有函数(让定义)?

dev*_*ium 20 .net f#

我想applyAndTruncate隐藏在外面的世界(也就是说,来自Scoring模块之外的任何东西),因为我真的只把它作为主干bestKPercentworstKPercent.有可能隐藏它吗?如果没有,F#是实现我想做的事情的方式是什么?

module Scoring
    let applyAndTruncate f percentage (scoredPopulation:ScoredPopulation) : ScoredPopulation =
      if (percentage < 0.0 || percentage > 1.0) then
        failwith "percentage must be a number between 0.0 and 1.0"

      let k = (int)(percentage * (double)(Array.length scoredPopulation))

      scoredPopulation
      |> f
      |> Seq.truncate k
      |> Seq.toArray

    let bestKPercent = applyAndTruncate sortByScoreDesc
    let worstKPercent = applyAndTruncate sortByScoreAsc
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 50

是.let private myfunc =会做的.

  • 对于递归函数,您应该将“rec”放在“private”之前 (2认同)

Ste*_*sen 12

您还可以使用签名文件来指定相应实现文件的公共接口.然后,我们的想法是,在实施固化之前,您不必担心可访问性.老实说,我从来没有使用它们,但它们被广泛用于F#编译器源代码(可能只是因为我对其他语言中使用的实现网站风格感到满意,而具有原始ML经验的人会很放心使用签名文件;此外,您还可以使用签名文件获得一些额外的功能,但没有什么超级引人注目的.

因此,如果您的Scoring模块是在一个名为的文件中实现的Scoring.fs,那么您将拥有一个相应的签名文件,其名称Scoring.fsi类似于:

namespace NS //replace with you actual namespace; I think you must use explicit namespaces
module Scoring =
    //replace int[] with the actual ScoredPopulation type; I don't think you can use aliases
    val bestKPercent : (float -> int[] -> int[])
    val worstKPercent : (float -> int[] -> int[])
Run Code Online (Sandbox Code Playgroud)

  • 如果我可以右键单击Visual Studio中的*.fs文件和"生成签名文件",我可能会使用签名文件.实际上,如果我想要一个,我要么必须查找语法来手动编写,要么我必须查找命令行参数让F#编译器为我生成一个,然后将文件添加到项目.到目前为止,我还没有感到需要足够的麻烦. (6认同)