Hi I’m trying to make my first little beginner actor but am not succeeding. I have no real goal other than to test some basic functions and get a feel for it. Not even sure the difference between an array and a list but was just trying to make a few list/arrays of type colors and just sort’ve add, merge, clear, etc. So idk if anyone knows how to add and query maybe to get me started that would be cool. I’ve been trying to look at example apps for guidance but some are more complicated than my level and others are just different and of course I’m trying to experiment and learn. Anyways any direction would be helpful and greatly appreciated!
// import Array "mo:base/Array";
import List "mo:base/List";
// type Iter<T> = Iter.Iter<T>;
// type Result<T,E> = Result.Result<T,E>;
// type UserId = Principal;
type Color = {
#red;
#orange;
#yellow;
#green;
#blue;
#purple;
};
// type Colors = [Color];
actor {
stable var pallete : [Color] = [];
flexible var tempPallete : [Color] = [#red];
// flexible var tempPallete : Colors = Array.init<Colors>(10, #red);
public query func getPalleteByIndex(n : Nat ) : async Color {
List.get<Color>(tempPallete : [Color], n);
};
// public query func getPallete() : async Colors {
// pallete
// };
// public query func getTempPallete() : async Colors {
// tempPallete
// };
// public func addToTemp(color: Color) : async Colors {
// Array.append<Colors>(tempPallete, [color]) : Colors;
// tempPallete
// };
// public func mergeTemp() : async Colors {
// Array.append<Colors>(pallete, tempPallete) : Colors;
// tempPallete := [];
// pallete
// };
// public func clearPallete() : async Colors {
// pallete := [];
// pallete
// };
}
Regarding list vs array -> Motoko’s arrays seem to be based on a classical array data structure, whereas its lists (including its associative list) seem to be using linked lists.
Honestly, because the libraries for Motoko are all so new, we don’t yet have satisfying data structure for relational data with the usual operations all there (products/joins, filters, sorting). Exploring that missing functionality is a big part of my motivation for writing the motoko-crud package, though it only supports non-relational databases at present.
For relational data, I would use nested Tries (the 2D and 3D versions, or deeper if necessary. The TrieMap module provides a wrapper that may get in the way for now.)
The Trie has nice support for joins, though I do not think your example query is doing any (right?).
All of the base collections support filtering and mapping, but not many support sorting (yet). RBTree is already sorted, but in this case (you only ask for LIMIT 100), you probably want to do something more intelligent, where you do not sort beyond the first 100 answers. That kind of functionality could be expressed with a lazy merge sort (lazily merging/sorting binary trees into lazy streams). I don’t have example code preprepared, but I can prepare some in the coming days. It’s part of what I want to do with motoko-crud anyway.