10

Is there a language which transpiles to JavaScript (like TypeScript, CoffeScript etc.) and is almost purely functional (like Haskell, Erlang, F#)?

I know there are libraries which helps maintain immutability (Underscore), and there are a lot of cool array functions and lambdas in ES6. But what I'm looking for is a functional like syntax and transpiler for it, so I can write functional JavaScript with not much effort.

godzsa
  • 2,105
  • 4
  • 34
  • 56

3 Answers3

11

Check out Scala.js, Elm or PureScript.

Other languages include ClojureScript (Clojure) and Funscript (F#)

Of those, I've only really used Scala.js and Elm, both of which work extremely well in my opinion.

Currying

In Elm and PureScript functions are curried by default. In Scala Currying can be done very easily by using multiple parameter lists:

def curried(param1: Int)(param2: Int): Int = ...
Community
  • 1
  • 1
Luka Jacobowitz
  • 22,795
  • 5
  • 39
  • 57
2

If you like F# you might like OCaml via js_of_ocaml (better OCaml compatibility) or BuckleScript (better JS interop) or even Reason ML (more modern syntax, including JSX).

If you like Haskell better, you could use GHCJS (it's Haskell, targeting JS), Purescript (strict evaluation means simpler JS), or Elm (who needs type classes anyway).

I'm less sure about Erlang. There seem to have been some attempts (possibly somewhat successful) at targeting JS, but I'm not at all familiar with any of them.

aij
  • 5,903
  • 3
  • 37
  • 41
  • Erlang can also be transpiled to JavaScript using [Erlscripten](https://github.com/erlscripten/erlscripten), which targets PureScript. – Anderson Green May 31 '22 at 23:48
1

If you are looking to stay at the .NET platform, and would want to use F# as your language to transpile to JavaScript, then I recommend looking at the following:

1.) Fable - Fable brings together the power of the F# compiler and Babel to make JavaScript a true backend for F#. It works directly on F# source code, no compilation needed.

2.) WebSharper - A fundamentally different web framework for developing functional and reactive .NET applications

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72