Function malachite_base::iterators::with_special_value
source · [−]pub fn with_special_value<I: Iterator>(
seed: Seed,
special_value: I::Item,
p_numerator: u64,
p_denominator: u64,
xs_gen: &dyn Fn(Seed) -> I
) -> WithSpecialValue<I>ⓘNotable traits for WithSpecialValue<I>impl<I: Iterator> Iterator for WithSpecialValue<I> where
I::Item: Clone, type Item = I::Item;
where
I::Item: Clone,
Expand description
An iterator that randomly produces another iterator’s values, or produces a special value.
Let $n_p$ be p_numerator
, $d_p$ be p_denominator
, and let $p=n_p/d_p$.
Every time a value is to be generated, the iterator returns the special value with probability $p$, or else returns a value from the inner iterator.
If $p > 0$, the output length is infinite. Otherwise, it is the same as the length of xs
.
Panics
Panics if p_denominator
is 0 or p_numerator
is greater than p_denominator
.
Examples
extern crate itertools;
use itertools::Itertools;
use malachite_base::iterators::{prefix_to_string, with_special_value};
use malachite_base::num::random::random_primitive_ints;
use malachite_base::random::EXAMPLE_SEED;
assert_eq!(
prefix_to_string(
with_special_value(EXAMPLE_SEED, -1i16, 1, 2, &random_primitive_ints::<i16>),
20
),
"[-1, -1, -1, 2901, -1, -14200, -1, -1, -1, -30997, -8245, -5338, -1, -1, -20007, -1, -1, \
-1, -1, -1, ...]"
);