ReasonML

image credits

Reason, also known as ReasonML, […] offers a syntax familiar to JavaScript programmers, and transpiles to OCaml. — Wikipedia: Reason

On heise there is a series of (German) articles about ReasonML by Marco Emrich: Funktional im Web unterwegs mit ReasonML. By the way, ML does not stand for Machine Learning, it has to do with the fact that ReasonML is based on OCaml. OCaml is based on Standard ML, a functional programming language from 1983. I think, if OCaml were a product and not a research project, it would probably be renamed ‘ocaML’ 😜. OCaml was also the basis for F# from Microsoft.

OCaml and JavaScript have functional and object-oriented aspects, which simplifies the syntax adaptation. But ReasonML is not the only functional programming language for JavaScript. There are already Elm and PureScript. But PureScript uses the Haskell syntax. The learning curve of Haskel is relatively high and then there is the thing with the monads 🙀. For this reason, ReasonML should be easier to learn.

ReasonML Example

ReasonML uses BuckleScript for compilation:

npm install -g bs-platform

Below is the code from the heise article that uses the idea of the stringCalc Kata. The definitions are described in the heise article (in German).

let splitByComma = str
  => str |> Js.String.split(",") |> Array.to_list;

let mapToInt = List.map(int_of_string);

let lessThan1000 = List.filter(x => x <= 1000);

let sum = List.fold_left((+), 0);

let stringCalc = str => str
  |> splitByComma
  |> mapToInt
  |> lessThan1000
  |> sum;

stringCalc("1,2005,3") |> Js.log

Bucklescript can create an own project. But you can also convert the ReasonML code to JavaScript as follows:

bsc stringcalc.re > stringcalc.js

… and this generates the following JavaScript Code:

// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE
'use strict';

var List = require("bs-platform/lib/js/list.js");
var $$Array = require("bs-platform/lib/js/array.js");
var Caml_format = require("bs-platform/lib/js/caml_format.js");

function splitByComma(str) {
  return $$Array.to_list(str.split(","));
}

function mapToInt(param) {
  return List.map(Caml_format.caml_int_of_string, param);
}

var lessThan1000 = List.filter((function (x) {
        return x <= 1000;
      }));

function sum(param) {
  return List.fold_left((function (prim, prim$1) {
                return prim + prim$1 | 0;
              }), 0, param);
}

function stringCalc(str) {
  var param = $$Array.to_list(str.split(","));
  return sum(lessThan1000(List.map(Caml_format.caml_int_of_string, param)));
}

console.log(stringCalc("1,2005,3"));

exports.splitByComma = splitByComma;
exports.mapToInt = mapToInt;
exports.lessThan1000 = lessThan1000;
exports.sum = sum;
exports.stringCalc = stringCalc;
/* lessThan1000 Not a pure module */

When executed with node stringcalc.js a 4 is shown, because 2005 is greater than 1000.

ReasonML Buch

There is a book by Axel Rauschmayer: Exploring ReasonML and functional programming. Without having read the book, I can say that it must be excellent, because I met Axel at Beyond Tellerand 2018 and he told me about ReasonML with great enthusiasm. This enthusiasm is definitely reflected in the book.

Some blog posts from Axel Rauschmayer about ReasonML:

Summary

I like the functional approach of ReasonML and I should definitely develop something with it.

Here are some more links from the heise article: