From 9f815d7fd6283cedbc49e3e2e54b9a9deb4e3202 Mon Sep 17 00:00:00 2001 From: ysndr Date: Thu, 10 Nov 2022 20:41:57 +0100 Subject: [PATCH 1/2] Impl unzip as fold --- src/process.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/process.rs b/src/process.rs index 988fc85f..bc67cd0b 100644 --- a/src/process.rs +++ b/src/process.rs @@ -20,16 +20,14 @@ impl Line { K: NumCast + Ord + Copy + Debug, V: NumCast + Copy + Debug, { - let (x, y) = hm - .into_iter() - .map(|(x, y)| { - let x = x.to_f64().ok_or(anyhow!("Failed casting {:?} to f64", x))?; - let y = y.to_f64().ok_or(anyhow!("Failed casting {:?} to f64", y))?; - Ok((x, y)) - }) - .collect::>>()? - .into_iter() - .unzip(); + let (x, y) = hm.into_iter().try_fold( + (Vec::with_capacity(hm.len()), Vec::with_capacity(hm.len())), + |(mut xs, mut ys), (x, y)| { + xs.push(x.to_f64().ok_or(anyhow!("Failed casting {:?} to f64", x))?); + ys.push(y.to_f64().ok_or(anyhow!("Failed casting {:?} to f64", y))?); + anyhow::Ok((xs, ys)) + }, + )?; Ok(Line { label: label.into(), From d8c52ebfbb89c00e606751a59de4a7951c72eedf Mon Sep 17 00:00:00 2001 From: Yannik Sander Date: Tue, 15 Nov 2022 16:05:10 +0100 Subject: [PATCH 2/2] Update src/process.rs Co-authored-by: Taeer Bar-Yam --- src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index bc67cd0b..eb8cd4d5 100644 --- a/src/process.rs +++ b/src/process.rs @@ -25,7 +25,7 @@ impl Line { |(mut xs, mut ys), (x, y)| { xs.push(x.to_f64().ok_or(anyhow!("Failed casting {:?} to f64", x))?); ys.push(y.to_f64().ok_or(anyhow!("Failed casting {:?} to f64", y))?); - anyhow::Ok((xs, ys)) + Ok((xs, ys)) }, )?;