Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
:timing
:sccache 1Tips and Traps¶
IterTools.combinations returns
an iterator adaptor that iterates over the k-length combinations of the elements from an iterator.
The iterator element type is Vec<Self::Item>.
This means that
Element of the original iterator need to be clonable.
For copy/clone-expensive types, it is better to use collect elements into a vector and generate indexes instead.
:dep itertools = "0.10.0"use itertools::Itertools;let it = (1..5).combinations(3);
itCombinations { indices: [0, 1, 2], pool: LazyBuffer { it: 4..5, done: false, buffer: [1, 2, 3] }, first: true }let v = it.collect::<Vec<_>>();
v[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]v[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]Note: Combinations does not take into account the equality of the iterated values!
{
let it = vec![1, 2, 2].into_iter().combinations(2);
it.collect::<Vec<Vec<_>>>()
}[[1, 2], [1, 2], [2, 2]](1..5).rev().combinations(3).collect::<Vec<Vec<_>>>()[[4, 3, 2], [4, 3, 1], [4, 2, 1], [3, 2, 1]]