Itertools::group_by是一个迭代器方法,每次键函数更改时都会生成一个新组.提供的示例演示了如何将它与for循环一起使用,但GroupBy在迭代器链中使用输出结构似乎非常麻烦,除非我误解了一些内容:
let data = vec![1, 3, -2, -2, 1, 0, 1, 2];
// example from docs
for (key, group) in &data.into_iter().group_by(|elt| *elt >= 0) {
assert_eq!(4, group.sum::<i32>().abs());
}
// usage in an iterator method chain
data.iter()
.group_by(|elt| **elt >= 0)
.into_iter()
.map(|bool, group| (bool, group.collect::<Vec<i32>>()))
.collect::<Vec<(bool, Vec<i32>)>>();
Run Code Online (Sandbox Code Playgroud)
第二个例子无法编译:
error[E0619]: the type of this value must be known in this context
--> src/main.rs:16:35
|
16 | .map(|bool, group| (bool, group.collect::<Vec<i32>>()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0599]: no method named `collect` found for type `std::iter::Map<itertools::Groups<'_, bool, std::slice::Iter<'_, i32>, [closure@src/main.rs:14:19: 14:35]>, [closure@src/main.rs:16:14: 16:63]>` in the current scope
--> src/main.rs:17:10
|
17 | .collect::<Vec<(bool, Vec<i32>)>>();
| ^^^^^^^
|
= note: the method `collect` exists but the following trait bounds were not satisfied:
`std::iter::Map<itertools::Groups<'_, bool, std::slice::Iter<'_, i32>, [closure@src/main.rs:14:19: 14:35]>, [closure@src/main.rs:16:14: 16:63]> : std::iter::Iterator`
`&mut std::iter::Map<itertools::Groups<'_, bool, std::slice::Iter<'_, i32>, [closure@src/main.rs:14:19: 14:35]>, [closure@src/main.rs:16:14: 16:63]> : std::iter::Iterator`
error[E0593]: closure takes 2 arguments but 1 argument is required
--> src/main.rs:16:10
|
16 | .map(|bool, group| (bool, group.collect::<Vec<i32>>()))
| ^^^ ------------------------------------------------- takes 2 arguments
| |
| expected closure that takes 1 argument
Run Code Online (Sandbox Code Playgroud)
我不相信我理解这个错误,但我认为编译器希望闭包的group参数具有这种显式类型:
itertools::Group<'_, bool, std::slice::Iter<'_, i32>, [closure@src\main.rs:26:15: 26:31]>
Run Code Online (Sandbox Code Playgroud)
在他们的代码中没有人想要它,所以我希望我误解了.
问题是,是否可能,以及如何在group_by没有for循环的情况下使用?
传递给的闭包map有错误的参数个数(两个而不是一个:改为使用元组); 错误信息当然不是很有帮助:)
此外,我不会将其bool用作变量名称,您需要在收集它们之前克隆整数引用(由于iter()vs. into_iter()).
extern crate itertools;
use itertools::Itertools;
fn main() {
let data = vec![1, 3, -2, -2, 1, 0, 1, 2];
let groups = data.iter()
.group_by(|elt| **elt >= 0)
.into_iter()
.map(|(ge0, group)| (ge0, group.cloned().collect()))
.collect::<Vec<(bool, Vec<i32>)>>();
println!("{:?}", groups);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1283 次 |
| 最近记录: |