• Stars
    star
    182
  • Rank 211,154 (Top 5 %)
  • Language
  • Created almost 7 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A cheat sheet for ReasonML -- WIP

ReasonML Cheat Sheet

ReasonML is a functional, strongly typed, programming language. It looks like JavaScript and compiles to JavaScript/Node as well as other languages. It is a dialect of OCaml and is compiled using BuckleScript. Therefore, you can use OCaml and BuckleScript APIs when using Reason.

Try it Out

If you want a more interactive example of ReasonML's language features, check out this link to the ReasonML REPL on their homepage: πŸ‘‰ Click here πŸ‘ˆ.

Comments

/* This is a comment whether single or multi-line */

Types

  1. Types are inferred. This means you don't have to declare types but you can if you want.
  2. Type coverage is 100%. Because types are inferred it means coverage is everywhere.
  3. Type system is sound. Compile and forget.

Types: list, array, string, char, bool, int, float, unit, option, etc.

Generics

Generics are created by putting a ' in front of an arbitrary variable name. However, it is most common to see single letters used.

let length: array('a) => int;

Generics allow you to use a function in a more generic way. In the case of length, it's a function which takes an array of any type, 'a, and returns an int. In this instance, 'a can be a string, an int, or a record. What it enforces is that the array contains only that type. It can either be array(int) or array(string) but not both.

Phantom Types

"A phantom type is a parametrised type whose parameters do not all appear on the right-hand side of its definition..." - Haskell Wiki, PhantomType

type formData('a) = string;

Here formData is a phantom type as the 'a parameter only appears on the left side.

References

Abstract Types

An abstract type is a type without a concrete definition.

module type Person = {
  type details;
  
  let person: details;
  let make: unit => details;
  let greet: details => string;
} 

From the example above, the module declares the details type but leaves it to the module to define its implementation.

module Person: Person = {
  type details = { name: string, age: int };
  
  let person = { name: "Adam", age: 31};
  let make = () => person;
  let greet = (details) => "Hello. My name is " ++ details.name ++ " and I am " ++ string_of_int(details.age) ++ " years old.";
}

let adam = Person.make();
let greet = Person.greet(adam);

References

List

Lists are created with square brackets:

let list: list(int) = [1, 2]

Spread

let list1 = [1, 2, 3];
let list2 = [0, ...list1];

Js.log(list2); /* [0, 1, 2, 3] */

* Can only use the spread operator once within a list

Rest

let list: list(int) = [1, 2, 3, 4];

let pm = (list): string => {
  switch(list) {
    | [] => "Empty"
    | [head, ...tail] => {j|I only want $head|j}
  }
};

Js.log(pm(list)); /* I only want 1 */

Array

Arrays are created with square brackets and pipes:

let arr: array(string) = [|"a", "b"|]

String

Strings are created with double quotes:

let str: string = "Yeah, to you it's Thanksgiving; to me it's Thursday"

Strings are concatenated with double plus signs:

let stringConcat: string = "Yo Adrian, " ++ "I did it!"

String interpolation comes with help from BuckleScript:

let rocky: string = "Rocky"
let mickey: string = "Mickey"

let scene: string = {j|$mickey makes $rocky chase chickens to get in shape.|j}

Char

Characters are created using single quotes:

let char: char = 'a';

Js.log(char); /* 97 */

Functions

Named

let someFunction = () => "someFunction";

Anonymous

let someFunction = () => {
  () => {
    "someAnonFunction"
  }
};

Js.log(someFunction()()); /* "someAnonFunction" */

Labeled Arguments

let labeledArgs = (~arg1, ~arg2) => {
  arg1 ++ arg2
};

Js.log(labeledArgs(~arg1="Some ", ~arg2="labeled args.")); /* "Some labeled args." */

Labeled As

let labeledArgs = (~arg1 as one, ~arg2 as two) => {
  one ++ two
};

Js.log(labeledArgs(~arg1="Some ", ~arg2="labeled args.")); /* "Some labeled args." */

Default and Optional Arguments

let labeledArgs = (~arg1="Some ", ~arg2=?, ()) =>
  switch (arg2) {
  | Some(arg) => arg1 ++ arg
  | None => arg1 ++ "labeled args."
  };

let res = labeledArgs(~arg2=?Some("labeled args"), ());

Js.log(res); /* Some labeled args. */

Partial Application

let labeledArgs = (~arg1 as one, ~arg2 as two) => {
  one ++ two
};

let firstArg = labeledArgs(~arg1="Some ");

let secondArg = firstArg(~arg2="labeled args.");

Js.log(secondArg); /* "Some labeled args." */

* Doesn't matter which order you pass the labeled argument

Destructuring

List

let [a, b]: list(int) = [1, 2];

Array

let [|a, b|]: array(int) = [|1 , 2|];

Tuple

let (a, b): (int, int) = (1, 2);

Pattern Matching

Use the switch to pattern match against values:

switch (value) {
  | 1 => "one"
  | _ => "Everything else"
};

Literal

switch (1) {
  | 1 => "one"
  | _ => "Everything else"
};

Option

switch (option) {
  | Some(option) => option
  | None => None
};

Destructuring

let arr: array(int) = [|1, 2, 3|];

let matchArr: array(int) => int = arr => {
  switch (arr) {
    | [|a, b, _|] => a + b
    | [|a, b|] => a
    | [|a|] => 0
    | [||] => -1
    | _ => 0
  };
};

Js.log(matchArr(arr)); /* 3 */

Guards

let arr = [|1, 2, 3|];

let matchArr: array(int) => int = (arr) =>
  switch arr {
  | [|a, b, _|] when a < 2 => a + b
  | [|a, _, _|] when a > 2 => a
  | [|a|] => 0
  | [||] => (-1)
  | _ => 0
  };

Js.log(matchArr(arr)); /* 3 */

Types

Literal

Record

type person = {
  firstName: string,
  lastName: string
};

let listOfPerson: list(person) = [{ firstName: "Rocky", lastName: "Balboa" }];

Closed Object

type obj = {
  .
  color: string,
};

let car: obj = {
  pub color = "Red"
};

Js.log(car#color); /* "Red" */

* Objects in Reason are like constructors in JS

Open Object

type obj('a) = {
  ..
  color: string,
} as 'a;

let car: obj({. color: string, isRed: unit => bool }) = {
  pub color = "Red";
  pub isRed = () => this#getColor() == "Red";
  pri getColor = () => this#color;
};

Js.log(car#isRed()); /* true */

JavaScript Object

type person = {
 .
 "name": string,
 [@bs.meth] "greet": unit => unit,
};

let printPerson = (p:person) => {
  Js.log(p##name);
  p##greet();
}

Tuple

type message: (int, string) = (200, "Success");

Tagged Union

type person =
  | Rocky
  | Mickey
  | Adrian;

let matchPerson: person => string = (who: person) =>
  switch who {
  | Rocky => "Rocky"
  | Mickey => "Mickey"
  | Adrian => "Adrian"
  };

Js.log(matchPerson(Rocky)); /* "Rocky" */

Operators

Symbol Meaning
+ integer addition
+. float addition
- integer subtraction
-. float subtraction
* integer multiplication
*. float mulitplication
/ integer division
/. float division
** exponentation
|> pipe / reverse application operator
@@ application operator
~+ unary addition integer
~+. unary addition float
~- unary negation integer
~-. unary negation float
++ string concatenation
@ list concatenation
-> pipe first

Helpful BuckleScript/OCaml Functions

Function Meaning
Js.log Logs value to console
Js.Float.isNaN Checks to see if value is NaN
string_of_int Convert integer into string
string_of_float Convert float to string

More Repositories

1

rbc-with-dnd-starter

A starter app for react-big-calendar with drag and drop addon. You can use as a template with the button `Use this template`
JavaScript
55
star
2

rbc-starter

A plain ole' react-big-calendar. You can use it as a template by clicking on the `Use this template` button.
JavaScript
38
star
3

tech-interview-projects

A list of take-home assessments for companies local and remote
34
star
4

reason-react-simon-game

The Simon Game...in ReasonReact
OCaml
9
star
5

full-stack-react-v3

A simple React and Redux Todo App w/ All the Latest Fixins
JavaScript
8
star
6

purescript-simon-game

Simon Game in PureScript
PureScript
7
star
7

purescript-in-react-example

JavaScript
5
star
8

purs-medellin-workshop

PureScript and Pux Workshop for JSConf Medellin
PureScript
5
star
9

reason-react-ssr-starter

A basic starter for a ReasonReact SSR app
JavaScript
4
star
10

react-redux-union

An async React + Redux + Union Types example
JavaScript
4
star
11

purescript-in-45-minutes

A short presentation on the PureScript language
PureScript
4
star
12

redux-union

Adding Union Types to Redux
JavaScript
3
star
13

bs-typing

BuckleScript bindings to the Typed.js library
OCaml
3
star
14

reason-react-starter

A simple reason react starter kit
JavaScript
3
star
15

fullstack-react-v2

A simple react CRUD app
JavaScript
3
star
16

cypress-testing-workshop

JavaScript
3
star
17

medium-demo-elm-local-weather-app

A super simple local weather app in Elm
Elm
2
star
18

reason-select-search

A select/search element made in ReasonML
OCaml
2
star
19

reason-react-medium-example

The reason-react app that accompanies a Medium article.
JavaScript
2
star
20

arecvlohe.github.io

Archive of my personal blog
MDX
2
star
21

GulpBuild

Simple build tool using Gulp, Jade, Stylus
CSS
1
star
22

customerio-assignment

TypeScript
1
star
23

js-course

Teaching JS to Bay Area youth at The Iron Yard
1
star
24

exercism-elixir

Elixir solutions to exercism.io problems
Elixir
1
star
25

Node

A simple node server that displays the weather for Tampa, FL. Tools used were: ES5, ES6, Node.js, Gulp, and Mocha.
JavaScript
1
star
26

ps-medium-simon-game

PureScript
1
star
27

learn-to-decode

Example encoders/decoders in ReasonML
OCaml
1
star
28

zero-grocery

TypeScript
1
star
29

elixir_elm_spa

Elixir
1
star