Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Please help to solve iterators5.rs #812

Closed
Leo777 opened this issue Aug 2, 2021 · 6 comments
Closed

Please help to solve iterators5.rs #812

Leo777 opened this issue Aug 2, 2021 · 6 comments

Comments

@Leo777
Copy link

Leo777 commented Aug 2, 2021

Hi stuck on this exercises could someone please help to solve it

@quangvdao
Copy link

quangvdao commented Aug 4, 2021

For the function insidemap( |(k,v)| ... ), you want a function that returns usize 1 if v == value and 0 otherwise. Then sum it all up.

@LeoniePhiline
Copy link

Alternatively, use filter():

map
    .values()
    .filter(|v| *v == &value)
    .count()

Note that in count_collection_iterator() you can call count_iterator() instead of re-implementing the functionality. Something like:

collection
    .iter()
    .map(|map| count_iterator(map, value))
    .sum()

@kenden
Copy link

kenden commented Sep 17, 2023

The Rustlings' hint is to use fold(). I would add why to the hint:
fold() is a particularly good choice for operations that require reducing a collection to a single value... such as counting the number of elements in a collection.
So, count_collection_iterator() could use fold() to reduce the collection of hashmaps to a single count of how many times the value appears in the hashmaps.

But... thinking of using fold() seems more difficult than thinking about using map().

And I would point to flatten() and flat_map() as part of the "advanced hint".

@benkeil
Copy link

benkeil commented Nov 3, 2023

from the hint:

For a further challenge, consult the documentation for Iterator to find
a different method that could make your code more compact than using fold.

@zabor2432
Copy link

zabor2432 commented Nov 7, 2023

For the fold approach you can think of something like that:

fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
    collection.iter().fold(0, |acc, x| acc   count_iterator(x, value))
}

@sotirr
Copy link

sotirr commented Nov 9, 2023

Both using fold:

fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
    // map is a hashmap with String keys and Progress values.
    // map = { "variables1": Complete, "from_str": None, ... }
    map.iter().fold(0, |acc, cur| if cur.1 == &value { return acc   1 } else { acc })
}
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
    // collection is a slice of hashmaps.
    // collection = [{ "variables1": Complete, "from_str": None, ... },
    //     { "variables2": Complete, ... }, ... ]
    collection.iter().fold(0, |acc, map| acc   count_iterator(map, value))
}

@rust-lang rust-lang locked and limited conversation to collaborators Jul 10, 2024
@mo8it mo8it converted this issue into discussion #2038 Jul 10, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants