Reduce problem
Someone that has a clean solution for this?
I have this data structure:
I want to get a response with two data structures (maps) mapped by stream name that separates completed streams and uncompleted streams where the keys are the stream names.
A stream is considered completed if all the posts in that stream are completed.
I guess a reduce would be the way to go but would need help with some specifics.
32 Replies
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Hmm almost but now the posts will be added to completed if the post is completed and to uncompleted if it isn't. It doesn't take into account if all of the posts in that stream are completed or not right?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
data like this for example:
This should result in
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I had a really ugly solution at first but I wasn't happy with that.
I basically created one map with all posts, one map with all completed posts and one map with all uncompleted posts grouped by stream name.
To check if it was a completed stream I just checked if the length of the same key in the completed post array for that key was as long as it was for allpost array for that key.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
yeah only issue I guess is that I don't get it mapped by stream name/id with that solution I just get all the posts for all the completed streams in one array and all the ones for uncompleted in the other one I guess or is the type wrong in the end? 🤔
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
The posts still contain their original data.
that is not mutated but the stream is considered uncompleted since all the posts in that stream aren't completed
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
@OrJDev thanks a lot for the help.
That seems to be pretty much what I'm looking for!
Closer to what I had done myself though but yours might be quite a lot more efficient.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Do you think this could be simplified further with a reduce btw or is this as low time complexity as possible? 🤔
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
because this is O(n^2) right?
@matee yeah using a reduce and Maps was what I thought would be the best solution but I'm not sure how to execute it.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
The time complexity for this is relatively important because there could possibly be 1000s of posts where each post is a document in db
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
and 5000x5000 and it's a lot of documents 😅
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
@OrJDev isn't his logic just O(n)
@matee Thanks a lot, is the time complexity of that just n? 😮
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
yeah I think so as well, really clean solution and thanks a lot to the both of you.
I'm so glad places like this exist ❤️
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
yeah I think Maps should be fine tbh.
I'm just gonna use the keys to display the names of the streams and values to display the posts
This is done on the first screen in my app so that's why it's important to get it right 😄
Would you memoize the results of this btw? 🤔
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
How do I best map through a map btw while still being able to use the key and values separately?
Would this be a good solution to render components based on uncompleted streams?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Oh yeah that's a way cleaner solution
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Didn't really figure out a solution with that that wouldn't become at least O(n^2)