Skip to content

Commit c9f9f5d

Browse files
authored
chore(motoko): migrate composite_query, parallel_calls, random_maze, classes, hello_cycles to mo:core 2.4.0 (#1325)
* chore(motoko): migrate composite_query, parallel_calls, random_maze to mo:core 2.4.0 - composite_query: RBTree → Map, ExperimentalCycles.add → (with cycles =), Array.init → VarArray.repeat, add mops.toml - parallel_calls: functional List → mutable List, Iter.range → Nat.range, add mops.toml - random_maze: Random.Finite → Random.AsyncRandom, Stack class → Stack module, List → pure/List, Array.init → VarArray.repeat, add mops.toml * chore: migrate random_maze, classes, hello_cycles to mo:core 2.4.0 - random_maze: remove unnecessary transient from constant let bindings - classes: replace mo:base RBTree+Array+ExperimentalCycles with mo:core Map+VarArray+Cycles; add persistent keyword; use (with cycles = N) syntax; add mops.toml - hello_cycles: replace mo:base imports with mo:core; add persistent keyword; use (with cycles = N) syntax and Cycles.accept<system>; add mops.toml * refactor: apply modern Motoko style via mops check --fix - Pin moc = "1.5.1" and enable warnings M0236/M0237/M0223 in all five example mops.toml files - classes/composite_query Buckets.mo: context dot for map.get/map.add, Nat import needed for implicit compare resolution - parallel_calls: context dot for l.add/l.values, c.toText(); explicit (): () return type on setup_callee; catch _ {} for unused e - random_maze: context dot for s.push/s.pop, us.isEmpty/us.size/us.get, cs.pushFront; redundant type annotations removed (M0223); irrefutable let pattern made exhaustive with else branch * docs: update READMEs and annotate mops.toml warning flags for mo:core examples * docs: remove stale alias note from Cycles import description
1 parent 9338618 commit c9f9f5d

16 files changed

Lines changed: 147 additions & 127 deletions

File tree

motoko/classes/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ The example defines two Motoko actors, `Map` and `Test`.
66

77
Map is a simple, distributed key-value store, mapping `Nat` to `Text` values, with entries stored in a small number of separate Bucket actors, installed on demand.
88

9-
`Map.mo` imports a Motoko actor class `Bucket(i, n)` from the library `Buckets.mo`. It also imports the `ExperimentalCycles` base library, naming it `Cycles` for short, to share its cycles amongst the buckets it creates.
9+
`Map.mo` imports a Motoko actor class `Bucket(i, n)` from the library `Buckets.mo`. It also imports `mo:core/Cycles` to share its cycles amongst the buckets it creates.
1010

1111
Each call to `Buckets.Bucket(n, i)` within Map instantiates a new Bucket instance (the i-th of n) dedicated to those entries of the Map whose key hashes to i (by taking the remainder of the key modulo division by n).
1212

1313
Each asynchronous instantiation of the Bucket actor class corresponds to the dynamic, programmatic installation of a new Bucket canister.
1414

15-
Each new Bucket must be provisioned with enough cycles to pay for its installation and running costs. Map achieves this by adding an equal share of Map's initial cycle balance to each asynchronous call to `Bucket(n, i)`, using a call to `Cycles.add(cycleShare)`.
15+
Each new Bucket must be provisioned with enough cycles to pay for its installation and running costs. Map achieves this by attaching an equal share of Map's initial cycle balance to each asynchronous call to `Bucket(n, i)`, using the syntax `await (with cycles = cycleShare) Buckets.Bucket(n, i)`.
1616

1717
The `Test.mo` actor imports the (installed) `Map` canister, using `Maps` Candid interface to determine its Motoko type. `Test`'s run method simply puts 24 consecutive entries into Map. These entries are distributed evenly amongst the buckets making up the key-value store. Adding the first entry to a bucket take longer than adding a subsequent one, since the bucket needs to be installed on first use.
1818

motoko/classes/mops.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Motoko dependencies (https://mops.one/)
22

3+
[toolchain]
4+
moc = "1.5.1"
5+
36
[dependencies]
4-
base = "0.14.9"
5-
core = "1.0.0"
7+
core = "2.4.0"
8+
9+
[moc]
10+
# M0236: use context dot notation (e.g. map.get(k) instead of Map.get(map, compare, k))
11+
# M0237: redundant explicit implicit arguments (e.g. Nat.compare is inferred automatically)
12+
# M0223: redundant type instantiation (e.g. Array.tabulate instead of Array.tabulate<T>)
13+
args = ["-W", "M0236", "-W", "M0237", "-W", "M0223"]

motoko/classes/src/map/Buckets.mo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import Nat "mo:base/Nat";
2-
import Map "mo:base/RBTree";
1+
import Map "mo:core/Map";
2+
import Nat "mo:core/Nat";
33

44
persistent actor class Bucket(n : Nat, i : Nat) {
55

66
type Key = Nat;
77
type Value = Text;
88

9-
transient let map = Map.RBTree<Key, Value>(Nat.compare);
9+
let map = Map.empty<Key, Value>();
1010

1111
public func get(k : Key) : async ?Value {
1212
assert ((k % n) == i);
@@ -15,7 +15,7 @@ persistent actor class Bucket(n : Nat, i : Nat) {
1515

1616
public func put(k : Key, v : Value) : async () {
1717
assert ((k % n) == i);
18-
map.put(k, v);
18+
map.add(k, v);
1919
};
2020

2121
};

motoko/classes/src/map/Map.mo

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Array "mo:base/Array";
2-
import Cycles "mo:base/ExperimentalCycles";
1+
import VarArray "mo:core/VarArray";
2+
import Cycles "mo:core/Cycles";
33
import Buckets "Buckets";
44

55
persistent actor Map {
@@ -14,7 +14,7 @@ persistent actor Map {
1414

1515
type Bucket = Buckets.Bucket;
1616

17-
let buckets : [var ?Bucket] = Array.init(n, null);
17+
let buckets : [var ?Bucket] = VarArray.repeat(null, n);
1818

1919
public func get(k : Key) : async ?Value {
2020
switch (buckets[k % n]) {
@@ -27,9 +27,7 @@ persistent actor Map {
2727
let i = k % n;
2828
let bucket = switch (buckets[i]) {
2929
case null {
30-
// provision next send, i.e. Bucket(n, i), with cycles
31-
Cycles.add(cycleShare);
32-
let b = await Buckets.Bucket(n, i); // dynamically install a new Bucket
30+
let b = await (with cycles = cycleShare) Buckets.Bucket(n, i);
3331
buckets[i] := ?b;
3432
b;
3533
};

motoko/classes/src/test/Test.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Debug "mo:base/Debug";
1+
import Debug "mo:core/Debug";
22
import Map "canister:map";
33

44
persistent actor Test {

motoko/composite_query/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ In detail, the example provides actor `Map`.
1616

1717
[Map.mo](./src/map/Map.mo) imports a Motoko _actor class_ `Bucket(i, n)`
1818
from library [Buckets.mo](./src/map/Buckets.mo).
19-
It also imports the `ExperimentalCycles` base library, naming it `Cycles` for short, to share its cycles amongst the buckets it creates.
19+
It also imports `mo:core/Cycles` to share its cycles amongst the buckets it creates.
2020

2121
Each call to `Buckets.Bucket(n, i)` within `Map` instantiates a new `Bucket` instance (the `i`-th of `n`) dedicated to those entries of the `Map` whose key _hashes_ to `i` (by taking the remainder of the key modulo division by `n`).
2222

2323
Each asynchronous instantiation of the `Bucket` actor class corresponds to the dynamic, programmatic installation of a new `Bucket` canister.
2424

2525
Each new `Bucket` must be provisioned with enough cycles to pay for its installation and running costs.
26-
`Map` achieves this by adding an equal share of `Map`'s initial cycle balance to each asynchronous call to `Bucket(n, i)`, using a call to `Cycles.add(cycleShare)`.
26+
`Map` achieves this by attaching an equal share of `Map`'s initial cycle balance to each asynchronous call to `Bucket(n, i)`, using the syntax `await (with cycles = cycleShare) Buckets.Bucket(n, i)`.
2727

2828
`Map`'s `test` method simply `put`s 16 consecutive entries into `Map`. These entries are distributed evenly amongst the buckets making up the key-value store. Adding the first entry to a bucket takes longer than adding a subsequent one, since the bucket needs to be installed on first use.
2929

motoko/composite_query/mops.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Motoko dependencies (https://mops.one/)
2+
3+
[toolchain]
4+
moc = "1.5.1"
5+
6+
[dependencies]
7+
core = "2.4.0"
8+
9+
[moc]
10+
# M0236: use context dot notation (e.g. map.get(k) instead of Map.get(map, compare, k))
11+
# M0237: redundant explicit implicit arguments (e.g. Nat.compare is inferred automatically)
12+
# M0223: redundant type instantiation (e.g. Array.tabulate instead of Array.tabulate<T>)
13+
args = ["-W", "M0236", "-W", "M0237", "-W", "M0223"]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import Nat "mo:base/Nat";
2-
import Map "mo:base/RBTree";
1+
import Map "mo:core/Map";
2+
import Nat "mo:core/Nat";
33

44
persistent actor class Bucket(n : Nat, i : Nat) {
55

66
type Key = Nat;
77
type Value = Text;
88

9-
transient let map = Map.RBTree<Key, Value>(Nat.compare);
9+
let map = Map.empty<Key, Value>();
1010

1111
public query func get(k : Key) : async ?Value {
1212
assert ((k % n) == i);
@@ -15,7 +15,7 @@ persistent actor class Bucket(n : Nat, i : Nat) {
1515

1616
public func put(k : Key, v : Value) : async () {
1717
assert ((k % n) == i);
18-
map.put(k, v);
18+
map.add(k, v);
1919
};
2020

2121
};

motoko/composite_query/src/map/Map.mo

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Debug "mo:base/Debug";
2-
import Array "mo:base/Array";
3-
import Cycles "mo:base/ExperimentalCycles";
1+
import Debug "mo:core/Debug";
2+
import VarArray "mo:core/VarArray";
3+
import Cycles "mo:core/Cycles";
44
import Buckets "Buckets";
55

66
persistent actor Map {
@@ -15,7 +15,7 @@ persistent actor Map {
1515

1616
type Bucket = Buckets.Bucket;
1717

18-
let buckets : [var ?Bucket] = Array.init(n, null);
18+
let buckets : [var ?Bucket] = VarArray.repeat(null, n);
1919

2020
public func getUpdate(k : Key) : async ?Value {
2121
switch (buckets[k % n]) {
@@ -36,8 +36,7 @@ persistent actor Map {
3636
let bucket = switch (buckets[i]) {
3737
case null {
3838
// provision next send, i.e. Bucket(n, i), with cycles
39-
Cycles.add(cycleShare);
40-
let b = await Buckets.Bucket(n, i); // dynamically install a new Bucket
39+
let b = await (with cycles = cycleShare) Buckets.Bucket(n, i); // dynamically install a new Bucket
4140
buckets[i] := ?b;
4241
b;
4342
};

motoko/hello_cycles/mops.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Motoko dependencies (https://mops.one/)
2+
3+
[toolchain]
4+
moc = "1.5.1"
5+
6+
[dependencies]
7+
core = "2.4.0"
8+
9+
[moc]
10+
# M0236: use context dot notation (e.g. map.get(k) instead of Map.get(map, compare, k))
11+
# M0237: redundant explicit implicit arguments (e.g. Nat.compare is inferred automatically)
12+
# M0223: redundant type instantiation (e.g. Array.tabulate instead of Array.tabulate<T>)
13+
args = ["-W", "M0236", "-W", "M0237", "-W", "M0223"]

0 commit comments

Comments
 (0)