码迷,mamicode.com
首页 > 其他好文 > 详细

[ReasonML] Workshops code

时间:2017-10-28 11:22:01      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:variant   use   mat   matching   extract   div   pre   entity   for   

/* 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 */
type event = {
  location: string,
  years: list int
};

/* Making a Reason record */
{location: "Bratislava", years: [2017, 2018]};

/** Our first Reason function, already?
 (Use ^ to join strings together) */
let rock song => Js.log ("We‘re all rocking to " ^ song);

/* Manually specifying types */
/* Use {j| ... |j} to interpolate strings */
let rock (song: string) (times: int) :string => 
{j|Rocked out to $(song) $(string_of_int times) times|j};

rock "Nad Tatrou sa blýska" 1; /* Invoke our function! */

/* Function with labelled arguments */
let rockLabelled ::songName ::times => 
  {j|Rocked out to $(song) $(string_of_int times) times|j};

rockLabelled songName::"Nad Tatrou sa blýska" times::1; /* Invoke our function with labelled arguments! */

/* Making a ReasonReact component */
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 */
type event = {
  location: string,
  years: list int
};

let event = {location: "Bratislava", years: [2017, 2018]};

let message = switch event {
| {location: "Bratislava", years} =>
  "This event was in Bratislava for " ^ (string_of_int (List.length years))
| {location, years: [2018, 2019]} => "This event was in " ^ location ^ " for 2018 and 2019"
| event => "This is a generic event"
};

/* Binding to JavaScript libraries */
/* From https://github.com/reasonml-community/bs-moment/blob/master/src/MomentRe.re */

external alert : string => unit = "alert" [@@bs.val];
external imul : int => int => int = "Math.imul" [@@bs.val];
external reverse : array ‘someKind => array ‘someKind = "" [@@bs.send];
let identity: ‘a => ‘a => ‘a = [%bs.raw {|function(x,y){/* Dangerous JavaScript! */ return x < y}|}];

alert "Bound successfully!";
imul 1 2;
reverse [|1, 2, 3|];
identity 1 2;

 

[ReasonML] Workshops code

标签:variant   use   mat   matching   extract   div   pre   entity   for   

原文地址:http://www.cnblogs.com/Answer1215/p/7745829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!