javascript developer friendly syntax let meaningoflife 41
play

JavaScript developer friendly syntax let meaningOfLife = 41 + 1; - PowerPoint PPT Presentation

JavaScript developer friendly syntax let meaningOfLife = 41 + 1; let add = (x, y) => x + y; add(2, 2); add(41, 1); let fruits = ["Apple", "Orange"]; if (true) { print_string("Hello World!"); }; OCaml


  1. JavaScript developer friendly syntax

  2. let meaningOfLife = 41 + 1;

  3. let add = (x, y) => x + y; add(2, 2); add(41, 1);

  4. let fruits = ["Apple", "Orange"];

  5. if (true) { print_string("Hello World!"); };

  6. OCaml semantics

  7. Reason Syntax OCaml Syntax OCaml AST

  8. Records

  9. let jane = {name: "Jane", age: 40};

  10. let jane = {name: "Jane", age: 40};

  11. type person = { name: string, age: int, }; let jane = {name: "Jane", age: 40};

  12. type person = { name: string, age: int, }; let jane = {name: "Jane", age: 40}; let tim = {...jane, name: "Tim"};

  13. Compiles to JavaScript

  14. Reason Syntax OCaml Syntax OCaml AST BuckleScript JavaScript

  15. Reason Syntax OCaml Syntax OCaml AST BuckleScript Native Code JavaScript

  16. Fromatter

  17. Reason Syntax OCaml Syntax OCaml AST BuckleScript Native Code JavaScript

  18. Reason Syntax OCaml Syntax refmt OCaml AST BuckleScript Native Code JavaScript

  19. Statically Typed Language

  20. Why yet another one?

  21. JavaScript const pieces = [ { kind: "king", color: "black", position: [3, 4] }, { kind: "pawn", color: "black", position: [4, 2] }, { kind: "knight", color: "white", position: [3, 3] } ];

  22. TypeScript interface Piece { kind: string; color: string; position: number[]; } const pieces = [ { kind: "king", color: "black", position: [3, 4] }, { kind: "pawn", color: "black", position: [4, 2] }, { kind: "knight", color: "white", position: [3, 3] } ]; const getKinds = (pieces: Piece[]) => pieces.map(piece => piece.kind);

  23. TypeScript interface Piece { kind: string; color: string; position: number[]; } const pieces = [ { kind: "king", color: "black", position: [3, 4] }, { kind: "pawn", color: "black", position: [4, 2] }, { kind: "knight", color: "white", position: [3, 3] } ]; const getKinds = (pieces: Piece[]) => pieces.map(piece => piece.kind);

  24. Reason type piece = { kind: string, color: string, position: (int, int), }; let pieces = [ {kind: "king", color: "black", position: (3, 4)}, {kind: "pawn", color: "black", position: (4, 2)}, {kind: "knight", color: "white", position: (3, 3)}, ]; let getKinds = pieces => List.map(item => item.kind, pieces);

  25. Variants

  26. type direction = | Up | Down | Left | Right;

  27. type direction = | Up | Down | Left | Right; let move = Left;

  28. type direction = | Up(int) | Down(int) | Left(int) | Right(int); let move = Left(2);

  29. type data = {names: list(string)}; type request = | Loading | Error(int) | Success(data);

  30. type color = Black | White; type kind = Queen | King | Rook | Bishop | Knight | Pawn; type piece = { color, kind, position: (int, int), }; let pieces = [ {kind: King, color: Black, position: (3, 4)}, {kind: Pawn, color: Black, position: (4, 2)}, {kind: Knight, color: White, position: (3, 3)}, ];

  31. type color = Black | White; type kind = Queen | King | Rook | Bishop | Knight | Pawn; type piece = { color, kind, position: (int, int), }; let pieces = [ {kind: King, color: Black, position: (3, 4)}, {kind: Pawn, color: Black, position: (4, 2)}, {kind: Knight, color: White, position: (3, 3)}, ];

  32. type color = Black | White; type kind = Queen | King | Rook | Bishop | Knight | Pawn; type piece = { color, kind, position: (int, int), }; let pieces = [ {kind: King , color: Black, position: (3, 4)}, {kind: Pawn , color: Black, position: (4, 2)}, {kind: Knight , color: White, position: (3, 3)}, ];

  33. Pattern Matching

  34. switch (<value>) { | <pattern1> => <case1> | <pattern2> => <case2> | ... };

  35. switch (1) { | 0 => "off" | 1 => "on" | _ => "off" };

  36. let displayText = switch (1) { | 0 => "off" | 1 => "on" | _ => "off" };

  37. type data = {names: list(string)}; type request = | Loading | Error(int) | Success(data); let ui = switch (Loading) { | Loading => "Loading ..." | Error(code) => "Something went wrong. Error: " ++ string_of_int(code) | Success(data) => List.fold_left((a, b) => a ++ b, "Names:", data.names) };

  38. type data = {names: list(string)}; type request = | Loading | Error(int) | Success(data); let ui = switch (Loading) { | Loading => "Loading ..." | Error(401) => "You aren’t authenticated." | Error(code) => "Something went wrong. Error: " ++ string_of_int(code) | Success(data) => List.fold_left((a, b) => a ++ b, "Names:", data.names) };

  39. type data = {names: list(string)}; type request = | Loading | Error(int) | Success(data); let ui = switch (Loading) { | Loading => "Loading ..." | Error(401 | 402) => "You aren’t authenticated." | Error(code) => "Something went wrong. Error: " ++ string_of_int(code) | Success(data) => List.fold_left((a, b) => a ++ b, "Names:", data.names) };

  40. “I call it my billion-dollar mistake …” – Tony Hoare

  41. Lesson I Don’t implement anything just because it’s easy!

  42. Lesson II Null is BAD!

  43. null; // doesn't exist!

  44. Option

  45. let foo = None; let foo = Some(42); let foo = Some([1, 2, 3]); let foo = Some("Hello World!");

  46. let foo = None; let foo = Some(42); let foo = Some([1, 2, 3]); let foo = Some("Hello World!"); switch (foo) { | None => "Sadly I don't know." | Some(value) => "It's " ++ value };

  47. Functions

  48. let add = (x, y) => x + y; add(2, 2); add(41, 1);

  49. let name = (~firstName, ~lastName) => firstName ++ " " ++ lastName; /* Jane Doe */ name(~firstName="Jane", ~lastName="Doe"); /* Jane Doe */ name(~lastName="Doe", ~firstName="Jane");

  50. ReasonReact

  51. Stateless Component Greeting.re let component = ReasonReact.statelessComponent("Greeting"); let make = (_children) => { ...component, render: _self => <h1>(ReasonReact.string("Hello"))</h1>, }; App.re ReactDOMRe.renderToElementWithId(<Greeting />, "root");

  52. Props Greeting.re let component = ReasonReact.statelessComponent("Greeting"); let make = (~name, _children) => { ...component, render: _self => <h1>(ReasonReact.string(“Hello " ++ name))</h1>, }; App.re ReactDOMRe.renderToElementWithId(<Greeting name="Helsinki" />, "root");

  53. Props Greeting.re let component = ReasonReact.statelessComponent("Greeting"); let make = ( ~name , _children) => { ...component, render: _self => <h1>(ReasonReact.string("Hello " ++ name))</h1>, }; App.re ReactDOMRe.renderToElementWithId(<Greeting name="Helsinki" />, "root");

  54. Props Greeting.re let component = ReasonReact.statelessComponent("Greeting"); let make = (~name, _children) => { ...component, render: _self => <h1>(ReasonReact.string("Hello " ++ name))</h1>, }; App.re ReactDOMRe.renderToElementWithId(<Greeting name="Helsinki" />, "root");

  55. type state = {count: int};

  56. type state = {count: int}; type action = | Add(int) | Reset;

  57. type state = {count: int}; type action = | Add(int) | Reset; let s = ReasonReact.string;

  58. type state = {count: int}; type action = | Add(int) | Reset; let s = ReasonReact.string; let component = ReasonReact.reducerComponent("Counter");

  59. type state = {count: int}; type action = | Add(int) | Reset; let s = ReasonReact.string; let component = ReasonReact.reducerComponent("Counter"); let make = _children => { ...component, initialState: () => {count: 0},

  60. type state = {count: int}; type action = | Add(int) | Reset; let s = ReasonReact.string; let component = ReasonReact.reducerComponent("Counter"); let make = _children => { ...component, initialState: () => {count: 0}, reducer: (action, state) => switch (action) { | Add(value) => ReasonReact.Update({count: state.count + value}) | Reset => ReasonReact.Update({count: 0}) },

Recommend


More recommend