File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -52,20 +52,16 @@ impl CandleSet {
5252 }
5353
5454 fn compute_average ( & mut self ) {
55- let mut sum = 0.0 ;
56- for candle in & self . candles {
57- sum += candle. close ;
58- }
59-
55+ let sum = self . candles . iter ( ) . fold ( 0.0 , |acc, c| acc + c. close ) ;
6056 self . average = sum / self . candles . len ( ) as f64 ;
6157 }
6258
6359 fn compute_min_and_max_values ( & mut self ) {
64- let mut sorted_candles = self . candles . clone ( ) ;
60+ self . max_value = self
61+ . candles
62+ . iter ( )
63+ . fold ( f64:: NEG_INFINITY , |a, b| a. max ( b. high ) ) ;
6564
66- sorted_candles. sort_by ( |a, b| a. low . partial_cmp ( & b. low ) . unwrap ( ) ) ;
67- self . min_value = sorted_candles. first ( ) . unwrap ( ) . low ;
68- sorted_candles. sort_by ( |a, b| b. high . partial_cmp ( & a. high ) . unwrap ( ) ) ;
69- self . max_value = sorted_candles. first ( ) . unwrap ( ) . high ;
65+ self . min_value = self . candles . iter ( ) . fold ( f64:: INFINITY , |a, b| a. min ( b. low ) ) ;
7066 }
7167}
You can’t perform that action at this time.
0 commit comments