Change ReasonLIGO and CameLIGO examples to use tuple param destructuring

This commit is contained in:
John David Pressman 2020-01-22 21:37:22 -08:00
parent eba4901a76
commit f17388bd80
7 changed files with 19 additions and 23 deletions

View File

@ -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-->
```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-->
```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);
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -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

View File

@ -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

View File

@ -182,14 +182,14 @@ function iter_op (const m : moveset) : unit is
<!--CameLIGO-->
```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-->
```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-->
```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-->
```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-->
```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-->
```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);
};

View File

@ -257,7 +257,7 @@ let sum_of_a_list: int = List.fold sum my_list 0
<!--ReasonLIGO-->
```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);
```

View File

@ -69,8 +69,7 @@ let check_hash_key (kh1, k2: key_hash * key) : bool * key_hash =
<!--ReasonLIGO-->
```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-->
```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);
};
```

View File

@ -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;
};