I'm rather new to F# and functional programming. I wrote the following method to concatenate some custom dsl scripts. The function works are expected without errors. I wanted to know if there was a better way to rewrite this function.
let foldScript (storeAppScripts : seq<string * string * string>) =
storeAppScripts
|> Seq.choose( fun (store, app, script) ->
match script with
| "" -> None
| _ -> Some (store, app, script) )
|> Seq.groupBy(fun (store, app, script) -> store)
|> Seq.map(fun (store, group) -> store, (group
|> Seq.groupBy(fun (_, app, _)-> app)
|> Seq.map(fun (app, group) -> app , (group |> Seq.fold(fun accum (_,_, script) -> sprintf "%s\r\n%s" accum script) ""))
|> Seq.map(fun (app, script) -> sprintf "\r\n\tuse Application = %s %s" app (script.ToString()))))
|> Seq.map(fun (store, script) ->
sprintf "use Store=%s%s" store (script |> Seq.fold( fun accum appScript -> sprintf "%s%s" accum appScript) ""))
|> Seq.fold(fun acc script -> sprintf "%s\r\n%s" acc script) ""
let s = [("s1", "a1", "script1"); ("s1", "a2", "script2"); ("s2", "a3", "script4")]
let r = foldScript s
//Expected result
// use Store=s1
// use Application = a1
// script1
// use Application = a2
// script2
// use Store=s2
// use Application = a3
// script1