Simplfy this
fold
call to all(|x| x == target)
66 rows.chain(columns)
67 .chain(diags)
68 .map(|triple| {
69 triple 70 .iter() 71 .map(|&i| self.get_state(i)) 72 .fold(true, |acc, x| acc && x == target) 73 })
74 .any(id)
75 }
Description
Certain forms of fold
have concise alternatives, such as sum
, product
,
any
or all
. Prefer these forms where applicable as they improve
readability.
The following forms of fold
can generally be simplified:
.fold(0, |acc, x| acc + x)
: use.sum()
.fold(1, |acc, x| acc * x)
: use.product()
.fold(false, |acc, x| acc || x)
: use.any(|x| x)
.fold(true, |acc, x| acc && x)
: use.all(|x| x)
Bad practice
let _ = (0..3).fold(false, |acc, x| acc || x > 2);
let _ = (0..3).fold(true, |acc, x| acc && x > 2);
let _: i32 = (0..3).fold(0, |acc, x| acc + x);
let _: i32 = (0..3).fold(1, |acc, x| acc * x);
Recommended
let _ = (0..3).any(|x| x > 2);
let _ = (0..3).all(|x| x > 2);
let _: i32 = (0..3).sum();
let _: i32 = (0..3).product();