Skip to content

Instantly share code, notes, and snippets.

@decatur
Created May 15, 2023 06:37
Show Gist options
  • Save decatur/1ee7074ce1599b965f8c313e80833f7e to your computer and use it in GitHub Desktop.
Save decatur/1ee7074ce1599b965f8c313e80833f7e to your computer and use it in GitHub Desktop.
Compute VWAP in rust
use itertools::put_back;
use itertools::Itertools;
fn main() {
let qp_it = put_back([(1, 2), (2, 2), (3, 2), (4, 3), (5, 2)].into_iter());
let acc_qp_it = qp_it.batching(|it| {
let mut q_acc = 0;
let mut qp_acc = 0;
while q_acc < 5 {
match it.next() {
None => break,
Some((q, p)) => {
if q_acc + q > 5 {
let carry = q_acc + q - 5;
it.put_back((carry, p));
q_acc = 5;
qp_acc += (q - carry) * p;
} else {
q_acc += q;
qp_acc += q * p;
}
}
}
}
if q_acc == 0 {
None
} else {
Some((q_acc, qp_acc))
}
});
println!("{:?}", acc_qp_it.format(" "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment