Crates.io | ordered_zip |
lib.rs | ordered_zip |
version | 0.1.0 |
source | src |
created_at | 2016-02-06 22:01:25.272489 |
updated_at | 2016-02-06 22:01:25.272489 |
description | An iterator that iterates two other iterators simultaneously, prioritized by pairwise ordering. |
homepage | |
repository | https://github.com/deepthought/ordered_zip |
max_upload_size | |
id | 4109 |
size | 17,876 |
An iterator that iterates two other iterators simultaneously, prioritized by pairwise ordering.
Sometimes there is the need to iterate over two sequences prioritized by the pairwise orderings of their items (such as for implementing operations on a sparse vector). The ordered_zip
crate aims to provide a versatile API for this.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
The ordered_zip
crate does not have any dependencies.
Add the following to your dependencies in your project's Cargo.toml
:
ordered_zip = "0.1.0"
… or whatever other version is more up-to-date.
Then add …
extern crate ordered_zip;
… and …
use ordered_zip;
… to your crate's root file (e.g. lib.rs
, main.rs
).
use std::cmp::Ordering;
use ordered_zip::Compare;
use ordered_zip::NonGreedy;
struct IndexCompare;
impl<T, U> Compare<(usize, T), (usize, U)> for IndexCompare {
fn cmp(lhs: &(usize, T), rhs: &(usize, U)) -> Ordering {
(lhs.0).cmp(&(rhs.0))
}
}
type NonGreedyStrategy = NonGreedy<IndexCompare>;
let v1 = vec![(0, 0.1), (1, 0.2), (3, 0.1), (4, 0.25), (7, 0.75)];
let v2 = vec![(2, 0.01), (4, 0.3), (5, 0.9), (9, 0.15), (10, 0.35)];
let nongreedy_zip = v1.into_iter().ordered_zip::<NonGreedyStrategy>(v2);
let dot_product = nongreedy_zip.fold(0.0, |sum, pair| {
sum + match pair {
(Some((_, l)), Some((_, r))) => l * r,
_ => 0.0,
}
}); // => 0.075
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
This project is licensed under the BSD License - see the LICENSE.md file for details.