Elm is too limited, Haskell seems too convoluted, unsure about F# and OCaml, don't know about good alternatives.
I've been considering PureScript because it seems to strike a balance between features, JS interoperability and skill transferability, but I'm still a little afraid of it being unnecessarily complex for my next short-term projects.
I've only now started considering ReasonML, but am I wrong to think it's much closer to the "get productive fast" side than the "long-term investment" one? Not to mention its JS-like syntax, which I admit I have a distaste for.
I was also overwhelmed by how convoluted the language seemed but eventually used it for a project, and my opinion completely changed. Haskell is a simple, beautiful language that enjoys great tooling.
Start out by learning just enough IO and monad stuff so that you can read input and pass it to your functions. This will not take long. Then you can gradually build up your knowledge of the language by creating modules that will become more and more idiomatic with time. You will discover parametrized types, laziness, and user-defined operators. At times you will struggle with how to use the type system to achieve your goal, but through practice, you will learn how it limits you, so you will know what approach to take.
Eventually you might notice that your code could be easier to read and write, and that’s where monads come in.
Somebody in this thread also recommended Clojure, which is a fine language that I would have also recommended a few months ago—it was my go-to language for spinning up servers or doing some computation. Now that I have tried Haskell though, I feel like I might not need to use Clojure again because Haskell is much more fun. So I’d hate for someone else to miss out on the fun like I did for years because they think they have to read a 600-page treatise just to get started! It’s very far from the truth.
That being said, all the choices you list (Elm, Haskell, F#, OCaml, PureScript) are solid languages with good communities. The core languages are similar enough that if you start learning one, the things you learn will transfer decently enough to all the others.
[1]: Haskell Programming from First Principles (http://haskellbook.com/)
It's a mature functional language with interesting properties, it's practical, data oriented. Libraries quality are top notch, you'll find no big framework but libraries working together to achieve what you want.
Some ClojureScript libraries are awesome, for example if you're on React you have Reagent or Rum that are fantastic libraries to work with React.
OP post could be written like this in Clojure (if you want to be explicit with all the screens):
(defn needs-cancel-button [screen]
(case screen
:screens/loading true
:screens/code-entry true
:screens/success false))
You'll notice that this is not typed but you can use Clojure spec to have some validation (this way being less explicit just returning false for the Success Screen, and true otherwise): (def screens #{:screens/loading :screens/code-entry :screens/success})
(defn needs-cancel-button
[screen]
{:pre [(s/valid? screens screen)]}
(case screen
:screens/success false
true))
Union types can be mimicked using spec, you'll find how here: https://lambdaisland.com/blog/25-09-2016-union-typesOCaml is also great, IMO you won't lose your time by learning these languages, you'll find some way to improve your current code
Whether you learn Clojure, OCaml, F#, Haskell, Elm or Haskell, you'll end up being a better developer.
(If it helps, I'm primarily a python/js developer before picking Clojure).
Clojure's data checking and testing stories are good too, with spec and the various test libs - including generative tests from specs. And of course you can share the same specs on front and back end, demonstrating a payoff of the front/back code sharing.
PureScript is pretty much haskell, it simply compiles to javascript -- wouldn't say there is much skill transferability in terms of language when coming from js, but it might make sense if you develop front-end.
Don't know about the other languages.
I did manage to bundle a ~32mb hello world with dependencies/runtime (with cross-compilation, yay!) - but I couldn't get giraffe or any other server (framework) running in a reasonable amount of time. (Side-note, I'd appreciate a pointer to a f# Web server stack that works in the above scenario - something light that's appropriate to build a json-api server in).
Many tutorials appeared to be a year out of date, with wrong details - leading to code snippets not working, things not building, dependencies not installing.
Since I know ocaml is solid, I now am a strong believer in reasonml (which fixes some of the oddities with ocaml, making it more pleasant - in a way a little more like standard ml).
Reasonml has a strong Web app side, and compiles to sane js.
The other great option is clojure/clojure script. It's been dogged a bit by jvm (startup) overhead - not sure how/if graal/truffle AOT compilation will improve that.
For immediate productivity, I would recommend Scala - it's run on the JVM, and lets you fall back to a fairly Java-like subset. It's ugly in some ways, but has a more mature web programming ecosystem than a lot of other functional programming languages - a decent backend framework in Play and Scala.js for maybe writing a few critical browser-side libraries - and has decent and growing adoption.
If you're not looking for quick productivity in web development, and don't care all that much about learning a language that's immediately applicable to existing job opportunities, I'd probably recommend the more "pure" modern languages like Haskell or OCaml; they're niche, but it's a stable niche, especially in more academic circles, and will give you a cleaner mental model of all the features that are hackily being tacked on in JS and Scala.
I've heard good things about Clojure (think LISP on the JVM), but haven't personally tried it and don't know how vibrant its community is. Having access to JVM libraries, as with Scala, is a great plus for immediate productivity.
The series of if checks (O(n)) it generates as opposed to a switch statement can really add up in loops with bigger enums.
Of the langs listed, ReasonML/OCaml have the best codegen/ffi story, it’s relatively easy to drop them into existing JS projects.
Longerm, in my views, means being able to write software for web, desktop, multiple smart-phone/table platforms, with a backend in, ideally, the same language/tooling as front end.
It seems like ReasonML is the only one that is targeting the above [1] (but I could be wrong). And that's because of its integration/support of ReactNative.
[1] https://medium.com/@peterpme/your-first-reasonml-pr-into-an-...
Elm, Purescript and ReasonML feel like much smaller communities that I wouldn't trust to necessarily make for a good long-term investment; Haskell is a bit too much of a jump to get productive in immediately. If you learn like I do - incrementally - then you want a strictly-evaluated, mainstream-ey language that allows you to write side effects without tracking them, just to start with.
Then it generates code that's better than Flow. It's not as aggressive as Reason for sure, but it's still pretty good.
https://www.typescriptlang.org/play/#src=const%20enum%20Scre...
https://www.typescriptlang.org/play/index.html#src=type%20Ap...
https://www.typescriptlang.org/play/#src=const%20enum%20Scre...
This is what uglify makes of that:
var needsCancelButton = function (e) {
switch (e) {
case 0:
case 1:
return!0;
case 2:
return!1
}
return e
};
EDIT: Just using `return screen` without the reassignment also works because the return type of the function and the type of the argument are incompatible, but that seems a lot more error prone and less widely applicable that explicitly reassigning to a `never` variable and returning that. > needsCancelButton(2)
false
Surely it should be: function needsCancelButton(n){ return 2 === n }
Also, more generally, uglify.js has no idea about the range of integers that could be passed in so how is a greater than optimization like that ever valid?In fact, Reason generates the `>=` comparison, and it knows it can do this because it know that a value greater than 2 can never happen.
The post should be updated to reflect this in a second!
Also note that, at least with Flow and TypeScript, type information isn't guaranteed to be correct, so it's a bit dangerous to do type-based optimizations (although tools certainly could try, with the caveat that if your types are wrong, your code may break even more than it otherwise would).
The key example to differentiate union and sum types is `int union int == int` whereas `int sum int` is something unique and different from `int`. A sum type keeps track of the "left side" versus the "right side".
So, all said and done not only is Reason producing faster, tinier code... it's also managing a more sophisticated and powerful abstraction!
Sum types are not more capable of an abstraction than union types. If we consider them in isolation, they are strictly less capable. For example, its possible to model sum types using union types in TypeScript by simply adding a tag field:
type Either<T, U> = { kind: 'left', left: T }
| { kind: 'right', right: U }
If you do this, typescript will also be able to do exhaustiveness checks provided its configured with noImplicitAny and strictNullChecks:For example, try removing a case item in the below code (paste it on http://www.typescriptlang.org/play/ and in options turn on the above mentioned checks):
type Stringifiable = { toString(): string };
function stringify<T extends Stringifiable, U extends Stringifiable>(thing: Either<T, U>):string {
switch (thing.kind) {
case 'left': return 'Left ' + thing.left.toString()
case 'right': return 'Right ' + thing.right.toString()
}
}
edit: additionally, PureScript row types are not strictly better. While row polymorphic records are a better tool to model records (they can model open or closed records and subtype relations are not needlessly complex), AFAIK PureScript still lacks advanced transformation tools such as mapped and conditional types which really make TS records shine (see https://www.typescriptlang.org/docs/handbook/release-notes/t... for example)Motivating example (given a record type, returns a record type containing only the properties that are not functions):
type NonFunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K
}[keyof T];
type NonFunctionProperties<T> = { [P in NonFunctionPropertyNames<T>]: T[P] };Mapped and conditional types add power to Typescript's repertoire, surely. On the other hand, this isn't a deficiency of _row_ types but instead the total position of Purescript's type system as opposed to Typescript. I just posit that row types are a nicer formulation for handling records than union/intersection atop sets of key mappings.
[1] https://medium.com/@javierwchavarri/performance-of-records-i...
You can actually create instances of PureScript types in JS and pass them into functions defined in PureScript. That explains why there's a JS class for each data constructor. Similarly, the generated PureScript code can't actually "know[] the match is exhaustive", since it could be called from anywhere.
The article was interesting, but I think the comparison would benefit from providing an example of the code generated for calling the function as well as its definition.
> enums, which are basically like Java’s enums.
They really aren't. Java enums are somewhat unique in that they are full-fledged classes, not just glorified constants. TS enums are much closer to those of C#.
var needsCancelButton = function(screen) { switch(screen) { case 0:case 1: return true; case 2: return false; } };
The Haxe code can be found here: https://try.haxe.org/#79e40
if(cancelButton == undefined) throw new Error("Specify if there should be a cancel button or not") let needsCancelButton = fun
| LoadingScreen | CodeEntryScreen => true
| SuccessScreen => false
};