F# - Organisation of algorithms in a file

119 views Asked by At

I do not find a good way to organize various algorithms. Today the file is like this :

1/ Extraction of values from Excel

2/ First algorithm based on these values (extracted from Excel) starting with "let matriceAlgo1 ="

3/ Second algorithm starting from the same values "let matriceAlgo2 ="

4/ Synthesis algorithm, doing a weighted average (depending on several values) of the 2/ and 3/ and selecting the result to be shown. "let matriceSynthesis ="

My question is the following : what should i put before the different parts of this file in order to just call them by there name ? I have seen answers explaining that Module could be an answer but I don't know how to apply it in my case (or anything else if it's not the good answer).At the end, I would like to be able to write something like this :

"launch Extraction

launch First Algorithm

launch Second Algorithm

Launch Synthesis"

1

There are 1 answers

0
Tomas Petricek On BEST ANSWER

The way I usually organize files is to have some clear visual separator between different sections of a file (see for example Crawler.fsx on GitHub) and then have one "main" section at the end that calls functions declared previously.

I don't really use modules unless I have a large number of functions with clashing names. It would be good idea to use modules if your algorithm consists of more functions (e.g. Alg1.initialize, Alg1.run, etc.). Then you could easily switch between using different algorithms using module alias:

module Alg = Alg1 // or Alg2

let a = Alg.initialize
Alg.run a

If the file is getting longer, then you could also move sections to separate files and use #load "File.fs" to load algorithms or functions from a file. In that case, you probably need to use modules, but you can always open the module after loading the file.