Skip to content

Instantly share code, notes, and snippets.

@sgrove
Last active May 12, 2018 11:08
Show Gist options
  • Save sgrove/a6474d26b77bbf29d19b66ad71527cc9 to your computer and use it in GitHub Desktop.
Save sgrove/a6474d26b77bbf29d19b66ad71527cc9 to your computer and use it in GitHub Desktop.
/* list of strings */
let _ = ["example-1", "example-2", "example-3"]; /* Array of strings */
let _ = [|"example-1", "example-2", "example-3"|]; /* Stating the type of a Reason record */
/* Defining our type */
type basicEvent = {
location: string,
years: list(int)
};
/* Making a Reason record, Reason knows this is a basicEvent */
{
location: "Vienna",
years: [2017, 2018]
};
/*** Our first Reason function, already?
(Use ++ to join strings together) */
let rock = (song) =>
/* Use {j| ... |j} to interpolate strings */
Js.log("We're all rocking to " ++ song);
/* Manually specifying types */
let rock = (songName, times) => {
let timesString = string_of_int(times);
{j|Rocked out to $(songName) $(timesString) times|j};
};
/* Invoke our function! */
rock("Und er lebte in der großen Stadt", 1);
/* Function with labelled arguments */
let rockLabelled = (~songName, ~times) => {
let timesString = string_of_int(times);
{j|Rocked out to $(songName) $(timesString) times|j};
};
/* Invoke our function with labelled arguments! */
rockLabelled(~songName="Und er lebte in der großen Stadt", ~times=1);
/* Making a ReasonReact component with raw function
MyComponent.make(
~foo=bar,
~children=[],
(), /* Making a ReasonReact component with JSX */
<MyComponent foo=bar />
);
*/
/* A variant animal type */
type animal =
| Dog
| Cat
| Bird;
/* Pattern matching our custom animal variant type */
let feed = (pet) =>
switch pet {
| Dog => "woof"
| Cat => "meow"
| Bird => "chirp"
};
/*** Destructuring combines code flow and extracts values at the same time,
let's do it here with a list of strings */
let names = ["Daniel", "Jared", "Sean"];
switch names {
| [] => "No names!"
| [personOne] => "One person in list, named " ++ personOne
| [personOne, personTwo] =>
"Two people in list, both " ++ personOne ++ " and " ++ personTwo
| [personOne, _, personThree, ...rest] =>
"At least three people in the list, but we picked out " ++ personOne ++ " and " ++ personThree
}; /* Destructuring a record type */
let event = {location: "Vienna", years: [2017, 2018]};
let message =
switch event {
| {location: "Vienna", years} =>
"This event was in Vienna for " ++ string_of_int(List.length(years)) ++ " years"
| {location, years: [2018, 2019]} =>
"This event was in " ++ location ++ " for 2018 and 2019"
| event => "This is a generic event"
}
/* From https://github.com/reasonml-community/bs-moment/blob/master/src/MomentRe.re */;
/* Binding to JavaScript libraries */
[@bs.val] external alert : string => unit = "alert";
[@bs.val] external imul : (int, int) => int = "Math.imul";
[@bs.send] external reverse : array('someKind) => array('someKind) = "";
let identity: ('a, 'a) => 'a = [%bs.raw
{|
function(x,y) {
/* Dangerous JavaScript! */
/* You're on your own here, the compiler will trust you! */
return x < y
}
|}
];
/* alert("Bound successfully!"); */
imul(1, 2);
reverse([|1, 2, 3|]);
identity(1, 2);
@filipewl
Copy link

Shouldn't the variable name here be songName instead of song?

@bburdiliak
Copy link

yes, you're right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment