Skip to content

Commit 4b2a73e

Browse files
committed
Merge upstream
2 parents 6ea43b8 + 3a28cb6 commit 4b2a73e

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

src/group_map.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ pub fn into_group_map<I, K, V>(iter: I) -> HashMap<K, Vec<V>>
1919
}
2020

2121
lookup
22-
}
22+
}
23+
24+
pub fn into_group_map_by<I, K, V>(iter: I, f: impl Fn(&V) -> K) -> HashMap<K, Vec<V>>
25+
where
26+
I: Iterator<Item=V>,
27+
K: Hash + Eq,
28+
{
29+
into_group_map(
30+
iter.map(|v| (f(&v), v))
31+
)
32+
}
33+
34+

src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,41 @@ pub trait Itertools : Iterator {
23322332
group_map::into_group_map(self)
23332333
}
23342334

2335+
/// Return an `Iterator` on a HahMap. Keys mapped to `Vec`s of values. The key is specified in
2336+
/// in the closure.
2337+
/// Different of into_group_map_by because the key is still present. It is also more general.
2338+
/// you can also fold the group_map.
2339+
///
2340+
/// ```
2341+
/// use itertools::Itertools;
2342+
/// use std::collections::HashMap;
2343+
///
2344+
/// let data = vec![(0, 10), (2, 12), (3, 13), (0, 20), (3, 33), (2, 42)];
2345+
/// let lookup: HashMap<u32,Vec<(u32, u32)>> = data.clone().into_iter().into_group_map_by(|a|
2346+
/// a.0);
2347+
///
2348+
/// assert_eq!(lookup[&0], vec![(0,10),(0,20)]);
2349+
/// assert_eq!(lookup.get(&1), None);
2350+
/// assert_eq!(lookup[&2], vec![(2,12), (2,42)]);
2351+
/// assert_eq!(lookup[&3], vec![(3,13), (3,33)]);
2352+
///
2353+
/// assert_eq!(
2354+
/// data.into_iter()
2355+
/// .into_group_map_by(|x| x.0)
2356+
/// .into_iter()
2357+
/// .map(|(key, values)| (key, values.into_iter().fold(0,|acc, (_,v)| acc + v )))
2358+
/// .collect::<HashMap<u32,u32>>()[&0], 30)
2359+
/// ```
2360+
#[cfg(feature = "use_std")]
2361+
fn into_group_map_by<K, V, F>(self, f: F) -> HashMap<K, Vec<V>>
2362+
where
2363+
Self: Iterator<Item=V> + Sized,
2364+
K: Hash + Eq,
2365+
F: Fn(&V) -> K,
2366+
{
2367+
group_map::into_group_map_by(self, f)
2368+
}
2369+
23352370
/// Constructs a `GroupingMap` to be used later with one of the efficient
23362371
/// group-and-fold operations it allows to perform.
23372372
///

src/sources.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,21 @@ impl<A, F> Iterator for RepeatCall<F>
7676
///
7777
/// use itertools::unfold;
7878
///
79-
/// let (mut x1, mut x2) = (1u32, 1u32);
80-
/// let mut fibonacci = unfold((), move |_| {
79+
/// let mut fibonacci = unfold((1u32, 1u32), |(x1, x2)| {
8180
/// // Attempt to get the next Fibonacci number
82-
/// let next = x1.saturating_add(x2);
81+
/// let next = x1.saturating_add(*x2);
8382
///
8483
/// // Shift left: ret <- x1 <- x2 <- next
85-
/// let ret = x1;
86-
/// x1 = x2;
87-
/// x2 = next;
84+
/// let ret = *x1;
85+
/// *x1 = *x2;
86+
/// *x2 = next;
8887
///
8988
/// // If addition has saturated at the maximum, we are finished
90-
/// if ret == x1 && ret > 1 {
91-
/// return None;
89+
/// if ret == *x1 && ret > 1 {
90+
/// None
91+
/// } else {
92+
/// Some(ret)
9293
/// }
93-
///
94-
/// Some(ret)
9594
/// });
9695
///
9796
/// itertools::assert_equal(fibonacci.by_ref().take(8),

0 commit comments

Comments
 (0)