Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting Composite function in f#

I have a recursive function in f# that iterates a string[] of commands that need to be run, each command runs a new command to generate a map to be passed to the next function.

The commands run correctly but are large and cumbersome to read, I believe that there is a better way to order / format these composite functions using pipe syntax however coming from c# as a lot of us do i for the life of me cannot seem to get it to work.

my command is :

 let rec iterateCommands (map:Map<int,string array>) commandPosition  = 
    if commandPosition < commands.Length then
        match splitCommand(commands.[0]).[0] with
        |"comOne" -> 
           iterateCommands (map.Add(commandPosition,create(splitCommand commands.[commandPosition])))(commandPosition+1)

The closest i have managed is by indenting the function but this is messy :

iterateCommands 
(map.Add
    (commandPosition,create
        (splitCommand commands.[commandPosition])
    )
) 
(commandPosition+1)

Is it even possible to reformat this in f#? From what i have read i believe it possible, any help would be greatly appreciated

The command/variable types are:
commandPosition - int
commands - string[]
splitCommand string -> string[]
create string[] -> string[]
map : Map<int,string[]>

and of course the map.add map -> map + x

like image 894
Matthew kingston Avatar asked Aug 17 '26 18:08

Matthew kingston


1 Answers

It's often hard to make out what is going on in a big statement with multiple inputs. I'd give names to the individual expressions, so that a reader can jump into any position and have a rough idea what's in the values used in a calculation, e.g.

let inCommands = splitCommand commands.[commandPosition]
let map' = map.Add (commandPosition, inCommands)
iterateCommands map' inCommands

Since I don't know what is being done here, the names aren't very meaningful. Ideally, they'd help to understand the individual steps of the calculation.

like image 67
Vandroiy Avatar answered Aug 19 '26 08:08

Vandroiy