All Projects β†’ arecvlohe β†’ Reasonml Cheat Sheet

arecvlohe / Reasonml Cheat Sheet

A cheat sheet for ReasonML -- WIP

Programming Languages

ocaml
1615 projects
reason
219 projects
bucklescript
41 projects

Projects that are alternatives of or similar to Reasonml Cheat Sheet

Stat Cookbook
πŸ“™ The probability and statistics cookbook
Stars: ✭ 1,990 (+964.17%)
Mutual labels:  cheatsheet
Es6 Cheatsheet
ES2015 [ES6] cheatsheet containing tips, tricks, best practices and code snippets
Stars: ✭ 12,673 (+6677.01%)
Mutual labels:  cheatsheet
Cheat Sheets
Cheat Sheets πŸ­πŸ€–πŸ‘€
Stars: ✭ 185 (-1.07%)
Mutual labels:  cheatsheet
Hackthebox
Notes Taken for HTB Machines & InfoSec Community.
Stars: ✭ 167 (-10.7%)
Mutual labels:  cheatsheet
Cehv10 Notes
πŸ“• Both personal and public notes for EC-Council's CEHv10 312-50, because its thousands of pages/slides of boredom, and a braindump to many
Stars: ✭ 170 (-9.09%)
Mutual labels:  cheatsheet
Javascriptcheatsheet
Quick reference to the tremendously accessible high-level language of the web ^_^
Stars: ✭ 175 (-6.42%)
Mutual labels:  cheatsheet
Rebolt
Framework for developing apps in ReasonML
Stars: ✭ 161 (-13.9%)
Mutual labels:  reasonml
Bs Material Ui
ReScript bindings for material-ui
Stars: ✭ 185 (-1.07%)
Mutual labels:  reasonml
Nginx Admins Handbook
How to improve NGINX performance, security, and other important things.
Stars: ✭ 12,463 (+6564.71%)
Mutual labels:  cheatsheet
Philip2
An Elm to OCaml compiler
Stars: ✭ 182 (-2.67%)
Mutual labels:  reasonml
Aboutsecurity
A list of payload and bypass lists for penetration testing and red team infrastructure build.
Stars: ✭ 166 (-11.23%)
Mutual labels:  cheatsheet
Ui Testing Cheat Sheet
How do I test this with UI Testing?
Stars: ✭ 2,039 (+990.37%)
Mutual labels:  cheatsheet
Python Cheatsheet
Collection of Python code snippets and cheatsheets (made for humans)
Stars: ✭ 176 (-5.88%)
Mutual labels:  cheatsheet
Fullstack Reason
A demo project that shows a fullstack ReasonML/OCaml app–native binary + webapp
Stars: ✭ 164 (-12.3%)
Mutual labels:  reasonml
Graphql Ppx
GraphQL language primitives for ReScript/ReasonML written in ReasonML
Stars: ✭ 185 (-1.07%)
Mutual labels:  reasonml
Ubuntu Cheatsheet
Ubuntu Terminal Cheatsheet
Stars: ✭ 161 (-13.9%)
Mutual labels:  cheatsheet
Pdb Cheatsheet
A cheatsheet for the Python Debugger (pdb)
Stars: ✭ 171 (-8.56%)
Mutual labels:  cheatsheet
Swift Cheat Sheet
A short guide to using Apple's new programming language, Swift.
Stars: ✭ 186 (-0.53%)
Mutual labels:  cheatsheet
Elispcheatsheet
Quick reference to the core language of Emacs ---Editor MACroS.
Stars: ✭ 186 (-0.53%)
Mutual labels:  cheatsheet
Matlabplotcheatsheet
A cheatsheet for those who plot with MATLAB
Stars: ✭ 177 (-5.35%)
Mutual labels:  cheatsheet

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].