|
1 | | -import List "mo:base/List"; |
2 | | -import Option "mo:base/Option"; |
3 | | -import Map "mo:base/OrderedMap"; |
4 | | -import Nat32 "mo:base/Nat32"; |
| 1 | +import Map "mo:core/Map"; |
| 2 | +import Nat32 "mo:core/Nat32"; |
5 | 3 |
|
6 | 4 | persistent actor Superheroes { |
7 | 5 |
|
8 | | - /** |
9 | | - * Types |
10 | | - */ |
11 | | - |
12 | | - // The type of a superhero identifier. |
13 | 6 | public type SuperheroId = Nat32; |
14 | 7 |
|
15 | | - // The type of a superhero. |
16 | 8 | public type Superhero = { |
17 | 9 | name : Text; |
18 | | - superpowers : List.List<Text> |
| 10 | + superpowers : [Text]; |
19 | 11 | }; |
20 | 12 |
|
21 | | - /** |
22 | | - * Application State |
23 | | - */ |
24 | | - |
25 | | - // The next available superhero identifier. |
26 | 13 | var next : SuperheroId = 0; |
27 | 14 |
|
28 | | - // The superhero data store. |
29 | | - transient let Ops = Map.Make<SuperheroId>(Nat32.compare); |
30 | | - var map : Map.Map<SuperheroId, Superhero> = Ops.empty(); |
31 | | - |
32 | | - /** |
33 | | - * High-Level API |
34 | | - */ |
| 15 | + let map = Map.empty<SuperheroId, Superhero>(); |
35 | 16 |
|
36 | | - // Create a superhero. |
37 | 17 | public func create(superhero : Superhero) : async SuperheroId { |
38 | 18 | let superheroId = next; |
39 | 19 | next += 1; |
40 | | - map := Ops.put(map, superheroId, superhero); |
| 20 | + Map.add(map, Nat32.compare, superheroId, superhero); |
41 | 21 | return superheroId |
42 | 22 | }; |
43 | 23 |
|
44 | | - // Read a superhero. |
45 | 24 | public query func read(superheroId : SuperheroId) : async ?Superhero { |
46 | | - let result = Ops.get(map, superheroId); |
47 | | - return result |
| 25 | + Map.get(map, Nat32.compare, superheroId) |
48 | 26 | }; |
49 | 27 |
|
50 | | - // Update a superhero. |
51 | 28 | public func update(superheroId : SuperheroId, superhero : Superhero) : async Bool { |
52 | | - let (result, old_value) = Ops.replace(map, superheroId, superhero); |
53 | | - let exists = Option.isSome(old_value); |
| 29 | + let exists = Map.get(map, Nat32.compare, superheroId) != null; |
54 | 30 | if (exists) { |
55 | | - map := result |
| 31 | + Map.add(map, Nat32.compare, superheroId, superhero); |
56 | 32 | }; |
57 | 33 | return exists |
58 | 34 | }; |
59 | 35 |
|
60 | | - // Delete a superhero. |
61 | 36 | public func delete(superheroId : SuperheroId) : async Bool { |
62 | | - let (result, old_value) = Ops.remove(map, superheroId); |
63 | | - let exists = Option.isSome(old_value); |
64 | | - if (exists) { |
65 | | - map := result |
66 | | - }; |
67 | | - return exists |
| 37 | + Map.delete(map, Nat32.compare, superheroId) |
68 | 38 | } |
69 | 39 | } |
0 commit comments