Logo

Programming-Idioms

History of Idiom 189 > diff from v29 to v30

Edit summary for version 30 by Plecra:
[Rust] Better use of combinators

Version 29

2020-07-12, 00:31:21

Version 30

2020-07-15, 18:31:38

Idiom #189 Filter and transform list

Produce a new list y containing the result of function T applied to all elements e of list x that match the predicate P.

Idiom #189 Filter and transform list

Produce a new list y containing the result of function T applied to all elements e of list x that match the predicate P.

Variables
y,T,e,x,P
Variables
y,T,e,x,P
Extra Keywords
map apply satisfy
Extra Keywords
map apply satisfy
Code
let y = x.iter()
	.filter_map(|e| if P(e) {Some(T(e))} else {None})
	.collect::<Vec<_>>();
Code
let y = x.iter()
	.filter(P)
        .map(T)
	.collect::<Vec<_>>();
Doc URL
https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#method.filter_map
Doc URL
https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#method.filter_map