1. /* list of strings */
  2. let _ = ["example-1", "example-2", "example-3"];
  3.  
  4. /* Array of strings */
  5. let _ = [|"example-1", "example-2", "example-3"|];
  6.  
  7. /* Stating the type of a Reason record */
  8. type event = {
  9. location: string,
  10. years: list int
  11. };
  12.  
  13. /* Making a Reason record */
  14. {location: "Bratislava", years: [2017, 2018]};
  15.  
  16. /** Our first Reason function, already?
  17. (Use ^ to join strings together) */
  18. let rock song => Js.log ("We're all rocking to " ^ song);
  19.  
  20. /* Manually specifying types */
  21. /* Use {j| ... |j} to interpolate strings */
  22. let rock (song: string) (times: int) :string =>
  23. {j|Rocked out to $(song) $(string_of_int times) times|j};
  24.  
  25. rock "Nad Tatrou sa blýska" 1; /* Invoke our function! */
  26.  
  27. /* Function with labelled arguments */
  28. let rockLabelled ::songName ::times =>
  29. {j|Rocked out to $(song) $(string_of_int times) times|j};
  30.  
  31. rockLabelled songName::"Nad Tatrou sa blýska" times::1; /* Invoke our function with labelled arguments! */
  32.  
  33. /* Making a ReasonReact component */
  34. MyComponent.make foo::bar children::[] ()
  35.  
  36. /* Making a ReasonReact component with JSX */
  37. <MyComponent foo={bar} />
  38.  
  39. /* A variant animal type */
  40. type animal =
  41. | Dog
  42. | Cat
  43. | Bird;
  44.  
  45. /* Pattern matching our custom animal variant type */
  46. let feed pet =>
  47. switch pet {
  48. | Dog => "woof"
  49. | Cat => "meow"
  50. | Bird => "chirp"
  51. };
  52.  
  53. /** Destructuring combines code flow and extracts values at the same time,
  54. let's do it here with a list of strings */
  55. let names = ["Daniel", "Jared", "Sean"];
  56.  
  57. switch names {
  58. | [] => "No names!"
  59. | [personOne] => "One person in list, named " ^ personOne
  60. | [personOne, personTwo] => "Two people in list, both " ^ personOne ^ " and " ^ personTwo
  61. | [personOne, _, personThree, ...rest] =>
  62. "At least three people in the list, but we picked out " ^ personOne ^ " and " ^ personThree
  63. };
  64.  
  65. /* Destructuring a record type */
  66. type event = {
  67. location: string,
  68. years: list int
  69. };
  70.  
  71. let event = {location: "Bratislava", years: [2017, 2018]};
  72.  
  73. let message = switch event {
  74. | {location: "Bratislava", years} =>
  75. "This event was in Bratislava for " ^ (string_of_int (List.length years))
  76. | {location, years: [2018, 2019]} => "This event was in " ^ location ^ " for 2018 and 2019"
  77. | event => "This is a generic event"
  78. };
  79.  
  80. /* Binding to JavaScript libraries */
  81. /* From https://github.com/reasonml-community/bs-moment/blob/master/src/MomentRe.re */
  82.  
  83. external alert : string => unit = "alert" [@@bs.val];
  84. external imul : int => int => int = "Math.imul" [@@bs.val];
  85. external reverse : array 'someKind => array 'someKind = "" [@@bs.send];
  86. let identity: 'a => 'a => 'a = [%bs.raw {|function(x,y){/* Dangerous JavaScript! */ return x < y}|}];
  87.  
  88. alert "Bound successfully!";
  89. imul 1 2;
  90. reverse [|1, 2, 3|];
  91. identity 1 2;

[ReasonML] Workshops code的更多相关文章

  1. 用code workshop取代code review

    Box Tech Blog » Effective learning through code workshops介绍了Box如何用code workshop而不是code review的形式来改善代 ...

  2. CV code references

    转:http://www.sigvc.org/bbs/thread-72-1-1.html 一.特征提取Feature Extraction:   SIFT [1] [Demo program][SI ...

  3. [REASONML] Using Javascript npm package from REASON

    For example, we want to use moment.js inside our ReasonML code. What we can do is create a module fi ...

  4. Visual Studio Code 代理设置

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...

  5. 我们是怎么做Code Review的

    前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...

  6. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  7. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  8. 在Visual Studio Code中配置GO开发环境

    一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github ...

  9. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

随机推荐

  1. C#版清晰易懂TCP通信原理解析(附demo)

    [转] C#版清晰易懂TCP通信原理解析(附demo) (点击上方蓝字,可快速关注我们) 来源:周见智 cnblogs.com/xiaozhi_5638/p/4244797.html 对.NET中网络 ...

  2. 洛谷 P1443 马的遍历

    P1443 马的遍历 题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入输出格式 输入格式: 一行四个数据,棋盘 ...

  3. 从头认识java-18.2 主要的线程机制(5)-守护线程与非守护线程

    这一章节我们来讨论一下守护线程与非守护线程. 1.什么是守护线程?什么是非守护线程? 非守护线程:Java虚拟机在它全部非守护线程已经离开后自己主动离开. 守护线程:守护线程则是用来服务用户线程的,假 ...

  4. 用react native 做的一个推酷client

    用react native 做的一个推酷client 仅供大家參考.仅仅为抛砖引玉.希望大家能以此来了解react.并编写出很多其它的优质的开源库,为程序猿做出贡献. 用的的组件: Navigator ...

  5. swift学习之数组

    首先数组的定义:以有序的方式存储同样类型的值 (1)数组的简写(shorthand)语法 你能够通过Array<Element>,在这里,Element时数组存储元素的值的类型.也能够通过 ...

  6. linux内核计算时间差以及jiffies溢出

    jiffies是每一个时钟中断,都会加1.这就导致一个问题.不管jiffies(一般来说是unsigned long类型)多少个字节,总有溢出的时候. 更极端的时候.当期jiffies是0xfffff ...

  7. Gitblit从一个服务器,迁移到另外一个服务器

    http://gitblit.com/federation.html A Gitblit federation is a mechanism to clone repositories and kee ...

  8. 关于vue中的语法糖v-model

    开发src-在线系统的过程中,封装了很多组件,如Dialog prompt等,在开源项目的组件中这些组件使用v-model来控制显示,我来总结一下关于自己学习到的v-model知识 1. 使用prop ...

  9. 记录一下 mysql 的查询中like字段的用法

    SELECT * from t_yymp_auth_role where role_name not like '%测试%' and role_name not like '%部门%' and rol ...

  10. Spatial Pyramid Matching

    转自:http://blog.csdn.net/jwh_bupt/article/details/9625469 SPM 全称是Spatial Pyramid Matching,出现的背景是bag o ...