ReasonML 101 notes

Reason 101:

variable assignment with let keyword cannot is immutable, but you can set it to mutable by assign to ref()

example JS:

let count = 0;
count ++;

in reason:

/* let count = 0; this is immutable */
let count = ref(count);

count := count ^ + 1;

for string concat:

let stringConcat = “some string” ++ variable ++ “another string“;

array in ReasonML is using [|”a”, “b”, “c”|] /* | = pipe symbol */

let test = [|
“a”,
“b”,
“c”
|];

and

list = [
“a”,
“b”,
“c”
];

list is immutable.

in list you can assign a new list like so,

let list = [
  “d”,
  …list
]; /* this is like a appending “d” to a create new list but this is not effective, so use list prime */
let list’ = [
  “d”,
  …list
];  /* notice the single quote beside the name, this is like slightly change of the list with new name list */

Array are really good at random access indexing and accessing
list is really good at appending and create a new list without change

function syntax is same as JS, however In reason everything is return automatically, let say:

let square = x => x * x;
/* this will return x * x * in reason;

Piping for function: piping is like take in the parameter and from left put in the right side…

“string” |> someFunction

this is like someFunction(“string”);

|> is old syntax which takes in the parameter and put it as last argument,

“string” |> someFunction(firstArgument);

and this is like someFunction(firstArgument, “string”);
where new syntax is which will take the parameter and put on left hand side, use |.

“string” |. someFunction(firstArgument);

which is like someFunction(“string”, firstArgument);

piping is really useful when you want to execute multiple level of functions:

example: someFunction(anotherFunction(moreFunction(“string”)))

this is so difficult to read, so do like this:

“string” |. moreFunction |. anotherFunction |. someFunction;

Statement shorthand:
if (x == y) ? true : false;
which is same in reason.

same time you can use if else in reason, however I realised you can’t use just if in reasonml, it has to go with else:

if (x == y) {
  true;
} else {
  false;
}

also in javascript if is statement which means you can’t assign to a variable with if statement, but in reason you can:

let checkIf =
    if (x == y) {
        true;
    } else {
        false;
    }

Pattern match is much:
switch x {
| y => true
| _ => false
}

_ is like all other case …

type variants are like list of variants types, this or that like constructors,

type days =
| Monday
| Tuesday
| Wednesday
….

required to use cap for the naming.

can be good for type checking

example :

switch (day) {
| Monday => “monday”
| Tuesday => “tuesday”
| Wednesday => “wednesday”
}

Published by max

Web developer and Interaction Designer base in Singapore