[ReasonML] Workshops code

复制代码
/* 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;
复制代码

 

posted @   Zhentiw  阅读(345)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-10-28 [Ionic2] Device Interaction in an Ionic App with Cordova Plugins
2016-10-28 [Angular2 Form] Angular 2 Template Driven Form Custom Validator
2016-10-28 [Angular2 Form] Model Driven Form Custom Validator
2016-10-28 [Angular2 Animation] Use Keyframes for Fine-Tuned Angular 2 Animations
2016-10-28 [Angular2 Animation] Delay and Ease Angular 2 Animations
2016-10-28 [Angular2 Animation] Control Undefined Angular 2 States with void State
2015-10-28 [Angular 2] Using Pipes to Filter Data
点击右上角即可分享
微信分享提示