I was using the strategy from this question's answer, and I was interested in producing another sequence that is a function of the two being created by the iterator map:
extern crate itertools_num;
use itertools_num::linspace;
fn main() {
// 440Hz as wave frequency (middle A)
let freq: f64 = 440.0;
// Time vector sampled at 880 times/s (~Nyquist), over 1s
let delta: f64 = 1.0 / freq / 2.0;
let time_1s = linspace(0.0, 1.0, (freq / 2.0) as usize)
.map(|sample| { sample * delta});
let (sine_440,
sine_100,
summed_signal): (Vec<f64>, Vec<f64>, Vec<f64>) =
time_1s.map(|time_sample| {
let sample_440 = (freq * &time_sample).sin();
let sample_100 = (100.0 * &time_sample).sin();
let summed_sample = &sample_440 + &sample_100;
(sample_440, sample_100, summed_sample)
}).unzip();
}
Like the closure indicates, the third signal is the sum of the previous two. What's baffling is the error I get:
error[E0271]: type mismatch resolving `<[closure@src/main.rs:17:21: 23:10 freq:_] as std::ops::FnOnce<(f64,)>>::Output == (_, _)`
--> src/main.rs:23:12
|
23 | }).unzip();
| ^^^^^ expected a tuple with 3 elements, found one with 2 elements
|
= note: expected type `(f64, f64, f64)`
= note: found type `(_, _)`
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::iter::Map<std::iter::Map<itertools_num::Linspace<f64>, [closure@src/main.rs:11:14: 11:40 delta:_]>, [closure@src/main.rs:17:21: 23:10 freq:_]>`
error[E0308]: mismatched types
--> src/main.rs:17:9
|
17 | time_1s.map(|time_sample| {
| ^ expected a tuple with 3 elements, found one with 2 elements
|
= note: expected type `(std::vec::Vec<f64>, std::vec::Vec<f64>, std::vec::Vec<f64>)`
= note: found type `(_, _)`
I could understand a type error, but why should the third tuple item just be completely ignored?