From f17388bd804406a6dd939b09ec2e7b4a40fc8e2f Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Wed, 22 Jan 2020 21:37:22 -0800 Subject: [PATCH] Change ReasonLIGO and CameLIGO examples to use tuple param destructuring --- gitlab-pages/docs/advanced/entrypoints-contracts.md | 13 ++++++------- gitlab-pages/docs/intro/what-and-why.md | 3 +-- gitlab-pages/docs/language-basics/functions.md | 4 ++-- gitlab-pages/docs/language-basics/maps-records.md | 12 ++++++------ .../docs/language-basics/sets-lists-tuples.md | 2 +- gitlab-pages/docs/language-basics/tezos-specific.md | 6 ++---- .../docs/language-basics/variables-and-constants.md | 2 +- 7 files changed, 19 insertions(+), 23 deletions(-) diff --git a/gitlab-pages/docs/advanced/entrypoints-contracts.md b/gitlab-pages/docs/advanced/entrypoints-contracts.md index eeb4fc0ae..51942c01b 100644 --- a/gitlab-pages/docs/advanced/entrypoints-contracts.md +++ b/gitlab-pages/docs/advanced/entrypoints-contracts.md @@ -50,8 +50,7 @@ let main (parameter, store: parameter * store) : operation list * store = ```reasonligo group=a type parameter = unit; type store = unit; -let main = (parameter_store: (parameter, store)) : (list(operation), store) => { - let parameter, store = parameter_store; +let main = ((parameter, store): (parameter, store)) : (list(operation), store) => { (([]: list(operation)), store); }; ``` @@ -93,7 +92,7 @@ let main (p, s: unit * unit) : operation list * unit = ```reasonligo group=b -let main = (p_s: (unit, unit)) : (list(operation), unit) => { +let main = ((p,s): (unit, unit)) : (list(operation), unit) => { if (amount > 0mutez) { (failwith("This contract does not accept tez"): (list(operation), unit)); } @@ -131,7 +130,7 @@ let main (p,s: unit * unit) : operation list * unit = ```reasonligo group=c let owner: address = ("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address); -let main = (p_s: (unit, unit)) : (list(operation), unit) => { +let main = ((p,s): (unit, unit)) : (list(operation), unit) => { if (source != owner) { (failwith("This address can't call the contract"): (list(operation), unit)); } @@ -230,10 +229,10 @@ type action = let dest: address = ("KT19wgxcuXG9VH4Af5Tpm1vqEKdaMFpznXT3": address); -let proxy = (param_s: (action, unit)): (list(operation), unit) => +let proxy = ((param, s): (action, unit)): (list(operation), unit) => let counter: contract(action) = Operation.get_contract(dest); - let op: operation = Operation.transaction(param_s[0], 0mutez, counter); - ([op], param_s[1]); + let op: operation = Operation.transaction(param, 0mutez, counter); + ([op], s); ``` diff --git a/gitlab-pages/docs/intro/what-and-why.md b/gitlab-pages/docs/intro/what-and-why.md index 4cddae9a7..fdd1d2887 100644 --- a/gitlab-pages/docs/intro/what-and-why.md +++ b/gitlab-pages/docs/intro/what-and-why.md @@ -107,8 +107,7 @@ type action = | Decrement(int) | Reset(unit); -let main = (p_s: (action, int)) : (list(operation), int) => { - let p, s = p_s; +let main = ((p,s): (action, int)) : (list(operation), int) => { let result = switch (p) { | Increment(n) => s + n diff --git a/gitlab-pages/docs/language-basics/functions.md b/gitlab-pages/docs/language-basics/functions.md index 6b5739205..5cf99f761 100644 --- a/gitlab-pages/docs/language-basics/functions.md +++ b/gitlab-pages/docs/language-basics/functions.md @@ -74,7 +74,7 @@ Currying is however *not* the preferred way to pass function arguments in CameLI While this approach is faithful to the original OCaml, it's costlier in Michelson than naive function execution accepting multiple arguments. Instead for most functions with more than one parameter we should place the arguments in a -[tuple](language-basics/sets-lists-touples.md) and pass the tuple in as a single +[tuple](language-basics/sets-lists-tuples.md) and pass the tuple in as a single parameter. Here's how you define a basic function that accepts two `ints` and returns an `int` as well: @@ -99,7 +99,7 @@ along with a return type. Here's how you define a basic function that accepts two `ints` and returns an `int` as well: ```reasonligo group=b -let add = (a: int, b: int) : int => a + b; +let add = ((a,b): (int, int)) : int => a + b; ``` The function body is a series of expressions, which are evaluated to give the return diff --git a/gitlab-pages/docs/language-basics/maps-records.md b/gitlab-pages/docs/language-basics/maps-records.md index 10b452c13..5f51de96c 100644 --- a/gitlab-pages/docs/language-basics/maps-records.md +++ b/gitlab-pages/docs/language-basics/maps-records.md @@ -182,14 +182,14 @@ function iter_op (const m : moveset) : unit is ```cameligo let iter_op (m : moveset) : unit = - let assert_eq = fun (i: address * move) -> assert (i.1.0 > 1) + let assert_eq = fun (i,j: address * move) -> assert (j.0 > 1) in Map.iter assert_eq m ``` ```reasonligo let iter_op = (m: moveset): unit => { - let assert_eq = (i: (address, move)) => assert(i[1][0] > 1); + let assert_eq = ((i,j): (address, move)) => assert(j[0] > 1); Map.iter(assert_eq, m); }; ``` @@ -209,14 +209,14 @@ function map_op (const m : moveset) : moveset is ```cameligo let map_op (m : moveset) : moveset = - let increment = fun (i: address * move) -> (i.1.0, i.1.1 + 1) + let increment = fun (i,j: address * move) -> (j.0, j.1 + 1) in Map.map increment m ``` ```reasonligo let map_op = (m: moveset): moveset => { - let increment = (i: (address, move)) => (i[1][0], i[1][1] + 1); + let increment = ((i,j): (address, move)) => (j[0], j[1] + 1); Map.map(increment, m); }; ``` @@ -243,14 +243,14 @@ function fold_op (const m : moveset) : int is ```cameligo let fold_op (m : moveset) : moveset = - let aggregate = fun (i: int * (address * (int * int))) -> i.0 + i.1.1.1 in + let aggregate = fun (i,j: int * (address * (int * int))) -> i + j.1.1 in Map.fold aggregate m 5 ``` ```reasonligo let fold_op = (m: moveset): moveset => { - let aggregate = (i: (int, (address, (int,int)))) => i[0] + i[1][1][1]; + let aggregate = ((i,j): (int, (address, (int,int)))) => i + j[1][1]; Map.fold(aggregate, m, 5); }; diff --git a/gitlab-pages/docs/language-basics/sets-lists-tuples.md b/gitlab-pages/docs/language-basics/sets-lists-tuples.md index 65cb1343e..f423caf7d 100644 --- a/gitlab-pages/docs/language-basics/sets-lists-tuples.md +++ b/gitlab-pages/docs/language-basics/sets-lists-tuples.md @@ -257,7 +257,7 @@ let sum_of_a_list: int = List.fold sum my_list 0 ```reasonligo group=b -let sum = (result_i: (int, int)): int => result_i[0] + result_i[1]; +let sum = ((result, i): (int, int)): int => result + i; (* Outputs 6 *) let sum_of_a_list: int = List.fold(sum, my_list, 0); ``` diff --git a/gitlab-pages/docs/language-basics/tezos-specific.md b/gitlab-pages/docs/language-basics/tezos-specific.md index 2e40d786a..5c9e0b1a6 100644 --- a/gitlab-pages/docs/language-basics/tezos-specific.md +++ b/gitlab-pages/docs/language-basics/tezos-specific.md @@ -69,8 +69,7 @@ let check_hash_key (kh1, k2: key_hash * key) : bool * key_hash = ```reasonligo -let check_hash_key = (kh1_k2: (key_hash, key)) : (bool, key_hash) => { - let kh1, k2 = kh1_k2; +let check_hash_key = ((kh1, k2): (key_hash, key)) : (bool, key_hash) => { let kh2 : key_hash = Crypto.hash_key(k2); if (kh1 == kh2) { (true, kh2); @@ -111,8 +110,7 @@ let check_signature (pk, signed, msg: key * signature * bytes) : bool = ```reasonligo -let check_signature = (param: (key, signature, bytes)) : bool => { - let pk, signed, msg = param; +let check_signature = ((pk, signed, msg): (key, signature, bytes)) : bool => { Crypto.check(pk, signed, msg); }; ``` diff --git a/gitlab-pages/docs/language-basics/variables-and-constants.md b/gitlab-pages/docs/language-basics/variables-and-constants.md index dadf5d84d..372f30180 100644 --- a/gitlab-pages/docs/language-basics/variables-and-constants.md +++ b/gitlab-pages/docs/language-basics/variables-and-constants.md @@ -108,7 +108,7 @@ with a new value being bound in place of the old one. ```reasonligo -let add = (a: int, b: int): int => { +let add = ((a,b): (int, int)): int => { let c: int = a + b; c; };