How to traverse inner array of a trie?

Hi,
In my case i have a function/API in my code.
Where i query a triemap and getting subset of the triemap.
This given function is a working one and i want to change it. here i am comparing the parameter dishTypeId to v.dishTypeId, this works well

public query func getDishesByDishTypeId (dishTypeId : Types.DishTypeId) : async [(Types.DishId, Types.Dish)] {
       let trieOfDishes = Trie.filter<Types.DishId, Types.Dish>(dishes, func (k, v) { v.dishTypeId == dishTypeId } );

       let arrayOfDishes : [(Types.DishId, Types.Dish)] = Iter.toArray(Trie.iter(trieOfDishes));

       return arrayOfDishes;
    };

here “v” represents one dish from dishes triemap

dish is having the below candid definition

public type Dish = {
        dishId : DishId;
        images : [Image];
        defaultImage : Image;
        name : Text;
        defaultName : Text;
        ingredientIds : [IngredientId];
        description : Text;
        mealTypeIds : [MealTypeId];
        cuisineId : CuisineId;
        dishTypeId : DishTypeId;
        alternativeNames : [AlternativeName];
        draft : Bool;
    };

but i have to check mealTypeId in the next function, so i have to somehow check whether the mealTypeIds inside dish (here “v”) contains the passed mealTypeId how to do this? any idea? i am stuck on this for past 1 week, i am not getting a clue from any of the examples and even from cancan project? any help would be appreciated. Thanks in advance

NB: i am expecting something similar to this but i can’t find any useful functions like contains from the docs

public query func getDishesByDishTypeId (mealTypeId : Types.MealTypeId) : async [(Types.DishId, Types.Dish)] {
       let trieOfDishes = Trie.filter<Types.DishId, Types.Dish>(dishes, func (k, v) { 

        v.mealTypeIds.contains(mealTypeId)  //how to do this in motoko?!

 } );

       let arrayOfDishes : [(Types.DishId, Types.Dish)] = Iter.toArray(Trie.iter(trieOfDishes));

       return arrayOfDishes;
    };
1 Like

With what’s currently in the base library, something like

Array.find(v.mealTypeIds, func(x : MealTypeId) { x == mealTypeId }) != null

would do the trick.

1 Like

This is what it is showing now should i type cast this or something? what am i doing wrong here?

The anonymous functions was missing a return type:

Array.find(v.mealTypeIds, func(x : MealTypeId) : Bool { x == mealTypeId }) != null
1 Like

Sorry, yes, what @quint said. Alternatively, you can explicitly instantiate the generic type of find, then the type of the anonymous function can be inferred from that:

Array.find<MealTypeId>(v.mealTypeIds, func(x) { x == mealTypeId }) != null
2 Likes

Thank you very much @rossberg @quint you guys are great. I am struggling to learn all these, where i can actually get an intermediate level of project to learn or any tutorial to start with?! any idea? Thank you very much for the help once again <3

You can find some great content to dive into in the examples repo, if it helps: https://github.com/dfinity/examples/tree/master/motoko

And the base library’s test dir can be useful too: https://github.com/dfinity/motoko-base/tree/master/test

There’s also a whole lot more on the hackathon developer resources page: https://support.dfinity.org/hc/en-us/articles/4965948618772-Supernova-Hackathon-2022-Developer-Resources