Make iterators 10X better with itertools
Ғылым және технология
Today we are learning about the itertools crate and how you can use it to make your iterators more powerful!
FREE Rust Cheat Sheet: letsgetrusty.com/cheatsheet
Code: github.com/letsgetrusty/itert...
Пікірлер: 51
📝Get your *FREE Rust cheat sheet* : www.letsgetrusty.com/cheatsheet
To convert between Results and Options within the same iterator combinator, you can use flatten() and flat_map() : I also prefer using Try_from() instead of Try_into() in oder to avoid the turbofish. When combining these two techniques, you can often avoid long closures, and sometimes just pass the function directly to the iterator combinator using the functionnal style. log.flatten() // Only keeps the happy paths : turns Ok(T) into T .map(|s| s.as_str()) .flat_map(ApacheLogEntre::try_from) // Equivalent to .map().flatten() chained together and uses functionnal syntax
Iterators are so powerful; wish people used them more. Back in Kotlin, I did some crazy cool stuff with them - mostly crazy 😅. They're just fun!
@kevin5475845
Жыл бұрын
i used to use them a lot in c# and seems pretty powerful here too. so far i'm a bit more of a newbie in rust
@briggsmiller7095
Жыл бұрын
I’ve been a Kotlin dev since 1.0. Had been doing Java since 1999. My only goal in my career now is to move to Rust. I want off this JVM (and all the awful legacy code that comes with it!)
@kevin5475845
Жыл бұрын
@@briggsmiller7095 nice, i used to program in java. first language i learnt and switched to c# during visual studio 2008 or so more when they stopped supporting J#. went to J# in between
@heavenstone3503
Жыл бұрын
@@kevin5475845 It's funny you say that because i think C# iterators are good but ultimately fall very short of what you can do in Rust At least in my Rust dev doing some C# opinion
@georgehelyar
Жыл бұрын
C# linq is great, but it's not zero cost. Rx (IObservable) and async streams (IAsyncEnumerable) are also great. I love the C# language too, but once you get to a certain level of production software, the runtime is just an exercise in fighting a garbage collector while keeping the memory usage low enough for a docker container. This is where rust really wins for me.
A more efficient version would be calling sorted() first then dedup() rather than unique(). This would produce the code that would be exactly like how you would write it manually: put everything into a vec first then sort it then remove any duplicates linearly in a single pass with O(1) space use.
@ShiyalaKohny
Жыл бұрын
Not necessarily, if you’re working with data that contains lots of duplicates, the minor space optimization of dedup would be offset by the added elements in the sort phase. Reduced time complexity may or may not be more important than reduced space complexity, depends on use case
@aperson4051
Жыл бұрын
@@ShiyalaKohnythis guy sorts!
@ShiyalaKohny
11 ай бұрын
@@aperson4051 I’m not sure I get your point. I’m saying that *because* he sorts, he should dedup before sorting, which saves time and is therefore more efficient (I can provide benchmarks to support that)
@aperson4051
11 ай бұрын
@@ShiyalaKohny it's a meme, giving you praise for your sorting knowledge. Nothing serious!
@kwinzman
8 ай бұрын
You probably want to first sort and then dedup because that will be faster because similar items will be next to each other.
Just wanted to thank you! All your videos have a great sound, like no mouth noises or lip-smack, this is really important to me. Thank you again!
Dude this video is really good. You mentioned a lot of stuff that I get hung up on when writing rust. Thanks!
just came here to thank your efforts for making cheat sheet. it saved my time and increased my pace to learn the lang
Doesn't chain() from std already provide the same functionality marge() gives?
Rubyists like me love that Rust can iterate over collections this easily.
@fabienpenso
Жыл бұрын
Agreed. All my rubyist friends love it as well.
Thanks, this video is a banger.
You could use the identity function from std::convert to annotate the type
super useful!
thank you ❤
very cool !!!!
Great!
The single most useful method in itertools is "format_with". Printing a formated string in rust with iterators can be very annoying if you want to avoid repeatedly allocating and deallocating memory for String. This method has a weird syntax but very useful to help you avoid allocations.
No one: literally no one: me who's portuguese looking at the thumbnail: EU SOU O BOB CONSTRUTOR, È O BOB TRABALHADOR
@leftyhero147
Жыл бұрын
kkkkkkk
Which visual studio extension you demo to import use? Thanks in advance
When calling sorted and unique, there two itération of the vec or there are combine in one iteration?
nice one
Itertools is great. Perhaps `rstest` next?
What happens if one date is corrupted? Whole line is dismissed or just the date is flagged as invalid?
I love functional approaches, but can't get over the inefficiency here 🐴 All those String allocations, and the HashSet inside unique. I'd either insert directly into a BTreeMap or into a HashSet and consume it into a Vec and sort it. (custom Hash implementation that only looks at timestamp, but Ord implementation that includes the strings? :v)
@praveenperera
Жыл бұрын
Premature optimization
@EgnachHelton
Жыл бұрын
Using a hashtable/btreeset would not be optimal for this. It's better to put them into a vector and sort the vector. Then you can deduplicate in one pass with O(1) space since all duplicates would be adjacent to each other after being sorted. In term of time complexity it's the same but using simpler data structures like vector is generally much faster than complexer ones like trees or hashtable. Calling .sorted().dedup().collect_vec() would exactly do that under the hood without extra allocations.
@qm3ster
Жыл бұрын
@@EgnachHelton I agree, as long as the data is realistic and not a degenerate case of all duplicates. Bravo!
At 0:30 I thought I spotted a bug with for i in vec and I think you wanted for i in iter. I put the code in the playground and no go. Vec doesn't implement the Copy trait rustc says. Oops my bad. Not a good example though hiding the into_iter context and move. If you try and access the vec after the loop boom!. Note a &vec is probably better used in the example.
You should probably call sorted() before you call unique()
CITIES WERE NOT SUPPOSED TO BE IN THE SKIES‼️‼️
personally i disagree with "first class iterator support". we can talk about that label when geneators become a stable feature.
My preferred strategy is to avoid the turbofish syntax. I would have put the type explicitly: let log1: ApacheLogEntry = log1.filter_map(|l| l.ok()?.as_ref().try_into().ok());
Why are you using try_into instead of try_from? Couldn't you just write `ApacheLogEntry::try_from(..).ok()`?