Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Automation] Enumerable map enhancements #148

Open
wants to merge 4 commits into
base: feature/automation-networks
Choose a base branch
from

Conversation

nizam-supraoracles
Copy link

}

/// Remove single Key from the Enumerable Map
public fun remove_value<K : copy+drop, V : store+drop+copy>(map: &mut EnumerableMap<K, V>, key: K) {
public fun remove_value<K: copy+drop, V: store+drop+copy>(map: &mut EnumerableMap<K, V>, key: K) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nizam-supraoracles I think it may be good to return the value being removed.

Comment on lines +163 to +171
public inline fun for_each_value<K: copy+drop, V: store+drop+copy>(set: &EnumerableMap<K, V>, f: |V|) {
let i = 0;
let len = length(set);
while (i < len) {
let key = *vector::borrow(&set.list, i);
f(table::borrow(&set.map, key).value);
i = i + 1
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need for_each_value_mut also where a mutable reference of the value is passed to the function.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add for_each_keyval, where copies of key and value is provided to function (original data structure must not change).

/// Filter the enumerableMap using the boolean function, removing all elements for which `p(e)` is not true.
public inline fun filter<K: copy+drop, V: store+drop+copy>(set: &EnumerableMap<K, V>, p: |&V|bool): vector<V> {
let result = vector<V>[];
for_each_value(set, |v| {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe for_each_value_ref would be better here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use that; however, since the next step involves using the vector::push_back method, we need to handle pointer dereferencing with vector::push_back(&mut result, *v). Therefore, there doesn't seem to be a specific reason to use for_each_value_ref in this context.

Copy link

@aregng aregng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
would it make sense to have filter_map option as well where it accepts 2 fucctions for filtering and then mapping the value and returns the vector of the mapped values.
this way for example automation_registry_state::get_active_task_ids can utilize this function.

@sjoshisupra
Copy link

Looks good. would it make sense to have filter_map option as well where it accepts 2 fucctions for filtering and then mapping the value and returns the vector of the mapped values. this way for example automation_registry_state::get_active_task_ids can utilize this function.

@nizam-supraoracles , I agree, this is a good suggestion. We can certainly have a map where a transformer function f: V -> T can be taken and it returns vector<T>. Try if we can also have a filter_map where it can take a predicate and a function. I am also not sure if two such lambda/closure are supported by Move. But if possible, we should have them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants