2011-09-11 39 views
17

我想讓applyAndTruncate對外界隱藏(也就是從Scoring模塊以外的任何東西),因爲我真的只用它作爲bestKPercentworstKPercent的骨幹。可以隱藏它嗎?如果沒有,那麼完成我想要做什麼的F#方法是什麼?是否可以在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 
+0

我幾乎不知道F#什麼,但也許[在F#spec](http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html#_Toc270597594)很有幫助。您似乎可以添加輔助功能註釋來讓綁定。 – 2011-09-11 15:45:08

回答

40

是的。 let private myfunc =將做到這一點。

+0

對於遞歸函數,您應該在'private'之前放置'rec'。 – knocte

10

您還可以使用signature files來指定相應實現文件的公共接口。然後,這個想法是,你不必擔心無障礙,直到實施已經鞏固。我老實說從未使用過它們,但它們被廣泛用於F#編譯器源代碼中(可能僅僅是因爲我對許多其他語言中使用的實現網站樣式感到滿意,而具有原始ML經驗的人員將會放心與簽名文件;還有you do get some extra features與簽名文件,但沒有什麼超級引人注目)。

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

如果我可以右鍵單擊Visual Studio中的* .fs文件和「生成簽名文件」,我可能會使用簽名文件。實際上,如果我想要一個,我必須查找語法來手工編寫它,或者我必須查找命令行參數以讓F#編譯器爲我生成一個,然後將該文件添加到項目。到目前爲止,我還沒有覺得需要足夠的麻煩來打擾。 –

相關問題