Remove folds (#10128)

automerge
This commit is contained in:
Greg Fitzgerald
2020-05-19 19:13:41 -06:00
committed by GitHub
parent 439fd30840
commit d9919b99d2
7 changed files with 52 additions and 54 deletions

View File

@ -760,9 +760,10 @@ pub fn fund_keys<T: Client>(client: &T, source: &Keypair, dests: &[Arc<Keypair>]
let mut retries = 0; let mut retries = 0;
let amount = chunk[0].1[0].1; let amount = chunk[0].1[0].1;
while !to_fund_txs.is_empty() { while !to_fund_txs.is_empty() {
let receivers = to_fund_txs let receivers: usize = to_fund_txs
.iter() .iter()
.fold(0, |len, (_, tx)| len + tx.message().instructions.len()); .map(|(_, tx)| tx.message().instructions.len())
.sum();
debug!( debug!(
" {} to {} in {} txs", " {} to {} in {} txs",
@ -854,9 +855,10 @@ pub fn create_token_accounts<T: Client>(
}) })
.collect(); .collect();
let accounts = to_create_txs let accounts: usize = to_create_txs
.iter() .iter()
.fold(0, |len, (_, tx)| len + tx.message().instructions.len() / 2); .map(|(_, tx)| tx.message().instructions.len() / 2)
.sum();
debug!( debug!(
" Creating {} accounts in {} txs", " Creating {} accounts in {} txs",

View File

@ -1227,12 +1227,14 @@ pub fn process_show_validators(
.current .current
.iter() .iter()
.chain(vote_accounts.delinquent.iter()) .chain(vote_accounts.delinquent.iter())
.fold(0, |acc, vote_account| acc + vote_account.activated_stake); .map(|vote_account| vote_account.activated_stake)
.sum();
let total_deliquent_stake = vote_accounts let total_deliquent_stake = vote_accounts
.delinquent .delinquent
.iter() .iter()
.fold(0, |acc, vote_account| acc + vote_account.activated_stake); .map(|vote_account| vote_account.activated_stake)
.sum();
let total_current_stake = total_active_stake - total_deliquent_stake; let total_current_stake = total_active_stake - total_deliquent_stake;
let mut current = vote_accounts.current; let mut current = vote_accounts.current;

View File

@ -42,7 +42,8 @@ pub fn calculate_non_circulating_supply(bank: Arc<Bank>) -> NonCirculatingSupply
let lamports = non_circulating_accounts_set let lamports = non_circulating_accounts_set
.iter() .iter()
.fold(0, |acc, pubkey| acc + bank.get_balance(&pubkey)); .map(|pubkey| bank.get_balance(&pubkey))
.sum();
NonCirculatingSupply { NonCirculatingSupply {
lamports, lamports,

View File

@ -209,7 +209,8 @@ fn graph_forks(bank_forks: &BankForks, include_all_votes: bool) -> String {
let total_stake = bank let total_stake = bank
.vote_accounts() .vote_accounts()
.iter() .iter()
.fold(0, |acc, (_, (stake, _))| acc + stake); .map(|(_, (stake, _))| stake)
.sum();
for (_, (stake, vote_account)) in bank.vote_accounts() { for (_, (stake, vote_account)) in bank.vote_accounts() {
let vote_state = VoteState::from(&vote_account).unwrap_or_default(); let vote_state = VoteState::from(&vote_account).unwrap_or_default();
if let Some(last_vote) = vote_state.votes.iter().last() { if let Some(last_vote) = vote_state.votes.iter().last() {

View File

@ -104,14 +104,12 @@ impl RemoteWalletManager {
let devices = usb.device_list(); let devices = usb.device_list();
let num_prev_devices = self.devices.read().len(); let num_prev_devices = self.devices.read().len();
let (detected_devices, errors) = devices let mut detected_devices = vec![];
.filter(|&device_info| { let mut errors = vec![];
for device_info in devices.filter(|&device_info| {
is_valid_hid_device(device_info.usage_page(), device_info.interface_number()) is_valid_hid_device(device_info.usage_page(), device_info.interface_number())
}) && is_valid_ledger(device_info.vendor_id(), device_info.product_id())
.fold( }) {
(Vec::new(), Vec::new()),
|(mut devices, mut errors), device_info| {
if is_valid_ledger(device_info.vendor_id(), device_info.product_id()) {
match usb.open_path(&device_info.path()) { match usb.open_path(&device_info.path()) {
Ok(device) => { Ok(device) => {
let mut ledger = LedgerWallet::new(device); let mut ledger = LedgerWallet::new(device);
@ -121,29 +119,21 @@ impl RemoteWalletManager {
ledger.pretty_path = info.get_pretty_path(); ledger.pretty_path = info.get_pretty_path();
let path = device_info.path().to_str().unwrap().to_string(); let path = device_info.path().to_str().unwrap().to_string();
trace!("Found device: {:?}", info); trace!("Found device: {:?}", info);
devices.push(Device { detected_devices.push(Device {
path, path,
info, info,
wallet_type: RemoteWalletType::Ledger(Arc::new(ledger)), wallet_type: RemoteWalletType::Ledger(Arc::new(ledger)),
}) })
} }
Err(err) => { Err(err) => {
error!( error!("Error connecting to ledger device to read info: {}", err);
"Error connecting to ledger device to read info: {}",
err
);
errors.push(err) errors.push(err)
} }
} }
} }
Err(err) => { Err(err) => error!("Error connecting to ledger device to read info: {}", err),
error!("Error connecting to ledger device to read info: {}", err)
} }
} }
}
(devices, errors)
},
);
let num_curr_devices = detected_devices.len(); let num_curr_devices = detected_devices.len();
*self.devices.write() = detected_devices; *self.devices.write() = detected_devices;

View File

@ -3,7 +3,6 @@
use byteorder::{ByteOrder, LittleEndian}; use byteorder::{ByteOrder, LittleEndian};
use solana_sdk::clock::Slot; use solana_sdk::clock::Slot;
use std::ops::Add;
#[derive(Default, Clone, Deserialize, Serialize)] #[derive(Default, Clone, Deserialize, Serialize)]
pub struct HardForks { pub struct HardForks {
@ -34,16 +33,17 @@ impl HardForks {
// The expected number of hard forks in a cluster is small. // The expected number of hard forks in a cluster is small.
// If this turns out to be false then a more efficient data // If this turns out to be false then a more efficient data
// structure may be needed here to avoid this linear search // structure may be needed here to avoid this linear search
let fork_count = self let fork_count: usize = self
.hard_forks .hard_forks
.iter() .iter()
.fold(0, |acc, (fork_slot, fork_count)| { .map(|(fork_slot, fork_count)| {
acc.add(if parent_slot < *fork_slot && slot >= *fork_slot { if parent_slot < *fork_slot && slot >= *fork_slot {
*fork_count *fork_count
} else { } else {
0 0
}
}) })
}); .sum();
if fork_count > 0 { if fork_count > 0 {
let mut buf = [0u8; 8]; let mut buf = [0u8; 8];

View File

@ -323,11 +323,13 @@ fn main() -> Result<(), Box<dyn error::Error>> {
let total_current_stake = vote_accounts let total_current_stake = vote_accounts
.current .current
.iter() .iter()
.fold(0, |acc, vote_account| acc + vote_account.activated_stake); .map(|vote_account| vote_account.activated_stake)
.sum();
let total_delinquent_stake = vote_accounts let total_delinquent_stake = vote_accounts
.delinquent .delinquent
.iter() .iter()
.fold(0, |acc, vote_account| acc + vote_account.activated_stake); .map(|vote_account| vote_account.activated_stake)
.sum();
let total_stake = total_current_stake + total_delinquent_stake; let total_stake = total_current_stake + total_delinquent_stake;
let current_stake_percent = total_current_stake * 100 / total_stake; let current_stake_percent = total_current_stake * 100 / total_stake;