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

时间:2022-02-17 17:04:13

I'd like to make applyAndTruncate hidden from the outside world (that is, from anything outside the Scoring module), as I really only use it as backbone for bestKPercent and worstKPercent. Is it possible to hide it? If not, what is the F#ish way of accomplishing what I want to do?

我想将applyAndTruncate隐藏在外部世界(即来自Scoring模块之外的任何东西),因为我实际上只将它用作bestKPercent和worstKPercent的主干。有可能隐藏它吗?如果没有,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

2 个解决方案

#1


42  

Yes. let private myfunc = will do it.

是。让private myfunc =会这样做。

#2


11  

You can also use signature files to specify the public interface of a corresponding implementation file. Then the idea is that you don't worry about accessibility until the implementation has solidified. I've honestly not ever used them, but they are used in the F# compiler source code extensively (probably just because I'm comfortable with the implementation site style used in so many other languages, whereas folks with original ML experience will be at ease with signature files; also, you do get some extra features with signature files, though nothing super compelling).

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

So if your Scoring module were implemented in a file named Scoring.fs, you'd have a corresponding signature file named Scoring.fsi that would look something like:

因此,如果您的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[])

#1


42  

Yes. let private myfunc = will do it.

是。让private myfunc =会这样做。

#2


11  

You can also use signature files to specify the public interface of a corresponding implementation file. Then the idea is that you don't worry about accessibility until the implementation has solidified. I've honestly not ever used them, but they are used in the F# compiler source code extensively (probably just because I'm comfortable with the implementation site style used in so many other languages, whereas folks with original ML experience will be at ease with signature files; also, you do get some extra features with signature files, though nothing super compelling).

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

So if your Scoring module were implemented in a file named Scoring.fs, you'd have a corresponding signature file named Scoring.fsi that would look something like:

因此,如果您的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[])