Instantly share code, notes, and snippets.

View person.js
var joe = {
name: "Joe",
age: 20,
job: "carpenter"
};
View person_new_valid.ml
(* Creating record *)
let joe = person ~name:"Joe" ~age:20 ~job:"Programmer"
let joeNameVerbose = name joe
let joeName = joe |. name
View person_new.ml
{name: "Joe", age: 20, job: "teacher"}
View person.ml
type person = {
name: string;
age: int;
job: string [@bs.optional];
} [@@bs.deriving abstract]
View map.ml
let mapping: string Js.Dict.t = Js.Dict.empty ()
(* Error: This expression has type int but an expression was expected of type *)
let () = Js.Dict.set mapping "some_key" 10
View module_syntax.txt
external <function name alias> :
<function type interface> =
<function name> [[@@bs.module "<module name>"]
View read_file.ml
external readFile :
name:string ->
([ `utf8
| `useAscii [@bs.as "ascii"]
] [@bs.string]) ->
(string -> string -> unit) ->
unit = ""
[@@bs.module "fs"]
let callback error data = Js.log(data)
View read_file.js
fs = require("fs");
fs.readFile("data.json", "utf8", (err, data) => {
console.log(data);
});
View normalize_factored.ml
let normalize s = Js.String.(
toLowerCase s |>
trim |>
replaceByRe [%bs.re "/ /g"] "_"
)
View normalize.ml
let normalize s =
Js.String.toLowerCase s |>
Js.String.trim |>
Js.String.replaceByRe [%bs.re "/ /g"] "_"