These are chat archives for rust-lang/rust

27th
Dec 2015
Grant Miner
@llambda
Dec 27 2015 21:05
why does map take a |n| but take_while takes a reference|&n| ?
oh...perhaps take_while only needs read-only access to its variable
panicbit
@panicbit
Dec 27 2015 21:13
llambda: The take_while closure needs to get a reference because otherwise you couldn't get to the elements after checking if you want to have them
Konstantin Stepanov
@kstep
Dec 27 2015 21:14
map() modifies the value, replacing old one with new one, you won't get old items after map() by definition.
panicbit
@panicbit
Dec 27 2015 21:14
In the case if map, the iterator doesn't need the original element anymore
Grant Miner
@llambda
Dec 27 2015 21:15
aah
Konstantin Stepanov
@kstep
Dec 27 2015 21:16
All filtering methods (filter and take_while) take reference, so it leaves old values intact, and you can get them afterwise.
Yes.
panicbit
@panicbit
Dec 27 2015 21:19
If the take_while closure wouldn't take a reference, then it would have to return the element back to the iterator, at which point you basically have map again
(because you wouldn't be forced to necessarily return the same element)
Peter Atashian
@retep998
Dec 27 2015 21:19
Hence why there is filter_map that combines filtering and mapping
panicbit
@panicbit
Dec 27 2015 21:20
TIL
retep998: That looks awfully similar to flat_map
Peter Atashian
@retep998
Dec 27 2015 21:22
@panicbit flat_map is used to flatten iterator nesting
panicbit
@panicbit
Dec 27 2015 21:22
retep998: Sure, but it could be used for the same purpose, eh?
retep998: You could even pass the same closure to both methods as far as I can see
(assuming they both return Option)
Peter Atashian
@retep998
Dec 27 2015 21:23
@panicbit Well since Option can be used as a crude iterator, technically flat_map can do what filter_map can do
But not the reverse
and filter_map might optimize better maybe, but it's definitely more clear what your code is doing
panicbit
@panicbit
Dec 27 2015 21:24
yeah, clarity and optimization, that's what I thought