Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Quality of Life Questions

I started coding in F# about 2 months ago.

I am greatly enjoying this programming language. I come from a C# background, and every time I need to revert back to C#, it feels so cumbersome and bloated.

But there are still things I think are problematic in F#, and this is what my questions are related to:

  1. There is no auto complete like VS has for C# right ? E.g. inside a function that takes parameter aParameter, if I write aPara no auto complete comes up. Is there a functionality inside VS that can solve this issue and which I am not aware of?

  2. Debugging is tedious to say the least. Since F# supports piping / chaining or whatever you want to call it, I typically try to chain as many things as possible (wherever it makes sense of course). Example:

    correctedData 
    |> List.filter (fun (_, r, _) -> r <= 3) 
    |> Seq.ofList 
    |> Seq.groupBy (fun (_, r, cti) -> (r,cti))                            
    |> Seq.map (fun ((r,cti),xs) -> (r, cti, Seq.length xs)) 
    |> Seq.toList
    

And this is just quarter of my whole chaining done. Whenever I mess something up in these chains, I find it very very hard to debug where it all went wrong.

Am I doing this chaining wrong (abusing it)? From my point of view, nothing intermediary from this chaining makes sense to exist atomically, thus there is no reason to have intermediary values. But because of this semantic point of view, I also lose the power of having intermediary values which help me debug. So then I have to insert them in the code, debug, then remove them again. But this is a wasted effort. Is there any way around this?

Also, debugging a List.map anonymous function inside a chain feels again awkward and hard compared to e.g. a for loop.

I am sure that I am missing something, and that my current way of debugging is probably not optimal - not by a long shot - so any suggestions are welcome.

like image 340
ImmoralWombat Avatar asked Jun 25 '15 08:06

ImmoralWombat


1 Answers

1.There is no auto complete like VS has for C# right

There is auto-complete for F#. It is not triggered automatically when you start typing though. If you're in Visual Studio and type aPara and then hit Ctrl+Space, it should be auto-completed to aParameter if it is in the scope. Similarly, you can do this in top-level scope to see available types and namespaces. Auto-completion also get triggered automatically when you type .

2.Debugging is tedious to say the least

I'd agree with this - debugging pipelines (especially with lazy sequences) is tricky. This is a bit confusing even when you're in C#, but C# does surprisingly good job on this one. There are two ways to deal with this:

  • Use F# Interactive more. I write most of my code in an F# Script file first, where you can run your partially complete solutions and see results immediately. For me, this pretty much replaces debugging, because by the time my code is complete, I know it works.

  • You can define a function tap that materializes the data in the pipeline and lets you see what is going through the pipe. I don't use this very much, but I know some people like it:

    let tap data = 
      let materialized = List.ofSeq data
      materialized
    

    Then you can use it in your pipeline:

    correctedData 
    |> List.filter (fun (_, r, _) -> r <= 3) 
    |> tap
    |> Seq.groupBy (fun (_, r, cti) -> (r,cti))                            
    |> tap
    |> Seq.map (fun ((r,cti),xs) -> (r, cti, Seq.length xs)) 
    |> Seq.toList
    

    This adds some noise to the pipeline, but you can remove it again once you're done with debugging.

like image 138
Tomas Petricek Avatar answered Oct 01 '22 16:10

Tomas Petricek