Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have private functions (let definitions) in F# modules?

Tags:

.net

f#

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?

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
like image 992
devoured elysium Avatar asked Sep 06 '25 12:09

devoured elysium


2 Answers

Yes. let private myfunc = will do it.

like image 131
Daniel Avatar answered Sep 08 '25 02:09

Daniel


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).

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:

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[])
like image 23
Stephen Swensen Avatar answered Sep 08 '25 02:09

Stephen Swensen