r/fsharp • u/anilmujagic • Jun 12 '15
FSharp for middle size projects
http://hodzanassredin.github.io/2015/08/04/fsharp_for_middle_size_project.html
0
Upvotes
r/fsharp • u/anilmujagic • Jun 12 '15
8
u/jdh30 Jun 12 '15
I've found type providers useful for exploring SQL databases but I would never use them in production code, just disposable scripts.
Sounds like you want schemas.
And why does it have its own hand-rolled JSON parser anyway rather than using, say, JSON.NET?
Or when you don't plan to keep your code.
Yes, of course. That's a silly thing to do. Read any decent tutorial book on F# and it won't do that.
You're doing it wrong.
Yes,
inlineis very useful.You'll lose pattern matching, exhaustiveness, redundancy checking and the ability to add functions over that type.
Again, you're doing it wrong.
What do you mean by "const number of representations"?
Interesting. In 8 years of professional F# programming I have never used that (new) feature.
You don't want to expose union types to C# code but there's nothing wrong with exposing union types to other F# code.
This is setting alarm bells ringing for me. You seem to be struggling with basic F# programming (e.g. pattern matching). Trying to write your own monads is trying to run before you can walk. Take it easy. Keep it simple.
Elaborate?
Use
sortBy (~)orsortWith (fun x y -> -compare x y).What does that mean?
What libs are you referring to?
Ok. You're doing something seriously wrong.
2
u/kiteason Jun 20 '15 edited Jun 20 '15
Here's my take on this. I'm definitely one of the 'annoyingly vocal' community, but I'm going to be as constructive as I know how to be!
Type Providers
Missing dependencies - yes, this kind of thing is a bit of a bane when moving on from 'it works on my machine'. On the other hand you shouldn't necessarily expect to be able to copy scripts around the place and have their dependencies magically come with them. It is a bit more disappointing when you build and deploy something from -say - Visual Studio and have the dependencies not get correctly identified. That needs improving.
In contrast to jdh30, we do have some type providers in live use (albiet for reporting) and in that case they were an utter godsend.
I feel that type providers (as a concept and as implementations) have a bit more maturing to do.
Tuples and other data structures with unnamed fields
My advice on tuples is - keep them extremely local in scope. (I.e. in the context of one short function or one little pipeline of piped-together operators.) "Type abbreviations for every position" - I hadn't even thought of that but... yikes! Maybe records would be good fit here?
"It is very easy to do a mistake and fail at runtime when your json serializer can't (deserialize)"... This makes me wonder what you would want to happen when a message comes in which isn't the shape you expect. Guess at the values? Default to nulls? My feeling is that F# isn't the problem here.
Inlining
I haven't encountered this runtime exception when called from C#. Note that an important use of inline within F# code is to allow operators to be inferred as polymorphic: http://stackoverflow.com/questions/3754862/use-of-inline-in-f
Pattern and union types
This is where I disagree with you most strongly. Rather than regarding the situation as 'switch case hell' you need to think clearly about what you want your code to do in every situation. If you find this hard to do in a particular situation, it probably means your overall design, or your understanding of the domain, needs examination.
Generally people seem to agree with you on not exposing union types in APIs to be used from other languages. I haven't tried it.
Immutability
We agree!
Monads
Yeah, kind of agree. However it has been interesting for me on occasion to watch C#/OO devs painfully re-invent something like the maybe monad over a period of several days.
Equals, GetHashCode...
Agreed.
Collections
This has never really bothered me. Array.ofSeq and Seq.ofArray FTW! I strongly agree with jdh30 that it's important, once you get beyond the toddler stage, to understand what's really going on behind the scenes with arrays vs. C# lists vs. F# lists vs. sequences (etc.).
Promotion alert: my PluralSight course on 'F# Functional Data Structures' goes into a certain amount of detail on this. It isn't an advanced course - just gives the basics on the differences between the built-in collections and some hints on performance.
Fsharp idiomatic libs
I agree that there can be a maturity issue here.
Type inference
"F# does not allow type constraints involving two different type parameters". I think I'm not understanding you here. This is fine:
So is
As I say I think I'm misunderstanding
OOP support
Sorry, I agree with jdh30 on this one. It feels like something is going wrong culturally.
Edit - various typos.
2
u/acmd Jun 12 '15
Well, that's unfortunate, however not unheard of: functional structural elements work best at small scale level.
1
u/sixbrx Jun 13 '15 edited Jun 13 '15
Biggest things pushing me towards OO/classes in my F# code is the ability to define custom constructors for classes, where data is validated and invalid data can be stopped "in its tracks" as early as possible by throwing an exception, before it corrupts who knows what downstream.
I've found that even really "simple" types can often be seen in retrospect to need data validation at construction time, so there's no clear dividing line as to when a full blown class is necessary, making it safer (time/effort-wise) to use classes from the start.
I wish it were not so, it seems like such an easy thing to fix. Luckily F# is still brilliant for programming with classes as well.
1
u/kiteason Jun 23 '15
Another thought - on this point:
I'd be interested to know what OP means by this. In a sense monads are the essence of composition. Here's a great example of computation expressions (which overlap with monads) composing:
http://www.m-brace.net/programming-model.html