2

I've recently studied the code of FParsec the F# port of Haskell Parsec parser combinators library.

FParsec public API is completely functional, but it relies on support library FParsecCS that relies on mutable data structures and performs explicit memory management using CLR unsafe constructs.

I think that this is done for performance reasons.

My experience with Functional Programming is still limited, so I'm asking to more experienced FP developers, if this could be a good technique to adopt in this scenario.

jay
  • 1,510
  • 2
  • 11
  • 19
  • 1
    What exactly is the question? Is it "are mutable arrays faster than immutable lists?". Or is it "Why do functional programmers sometimes use mutation?" – John Palmer Apr 22 '13 at 10:00
  • 3
    If your question is, why FParsec is internally based on imperative/mutable constructs, the answer is: **performance**. There are many articles where [Stephan Tolksdorf](http://stackoverflow.com/users/558823/stephan-tolksdorf) discusses those issues. See, for example, [this answer](http://stackoverflow.com/a/12849953/974789) or [Where is the Monad?](http://www.quanttec.com/fparsec/users-guide/where-is-the-monad.html). There's no objective answer if you should take this approach in your projects. My suggestion is to do it in an academic manner first and only then improve performance when needed. – Be Brave Be Like Ukraine Apr 22 '13 at 11:08
  • 1
    +1 @bytebuster catched the point. If it's a common technique, I'm also agree to stay away from __premature optimization__. – jay Apr 22 '13 at 13:54
  • Your question seems to trail off at the end, you may want to add an actual question at the end. Such as "are mutable inner workings of immutable classes good practice?" – Guvante Apr 26 '13 at 23:33

1 Answers1

4

Generally speaking you should stick with a more functional style. A few exceptions are:

  1. Public Libraries (for performance reasons)
  2. Performance Critical sections of your app
  3. Apis consumed by other languages (functional apis may be more difficult for non-functional languages to consume)

Even for these exceptions, frequently there is a functional means of doing what you're trying to do with a functional style (api definitions excluded), so make sure you explore your functional options before turning to a more imperative or OO style.

N_A
  • 19,799
  • 4
  • 52
  • 98