英文:
A nicer way to create an ad hoc iterator than "vec![a,b,c].into_iter()"?
问题
The expression vec![a, b, c].into_iter()
seems unnecessarily long. Is there something better?
The doc for itertools kmerge gives this example:
use itertools::Itertools;
let a = (0..6).step(3);
let b = (1..6).step(3;
let c = (2..6).step(3);
let it = vec![a, b, c].into_iter().kmerge();
itertools::assert_equal(it, vec![0, 1, 2, 3, 4, 5]);
When you just want to create an ad hoc iterator of values, is there anything better? Maybe something like iter![a,b,c]
(if iter
is taken, then some other word). If this is possible, I've got to believe that someone's already done it, but I can't find anything.
英文:
The expression vec![a, b, c].into_iter()
seems unnecessarily long.Is there something better?
The doc for itertools kmerge gives this example:
use itertools::Itertools;
let a = (0..6).step(3);
let b = (1..6).step(3);
let c = (2..6).step(3);
let it = vec![a, b, c].into_iter().kmerge();
itertools::assert_equal(it, vec![0, 1, 2, 3, 4, 5]);
When you just want to create an ad hoc iterator of values, is there anything better?
Maybe something like iter![a,b,c]
(if iter
is taken, then some other word).
If this is possible, I've got to believe that someone's already done it, but I can't find anything.
答案1
得分: 4
数组与Vec
同样有效,避免了堆分配。
[a, b, c].into_iter()
也许文档是在Rust 1.53之前编写的,当时这个功能已经稳定。
英文:
An array works just as well as a Vec
and avoids the heap allocation.
[a, b, c].into_iter()
Perhaps the documentation predates Rust 1.53 when this was stabilized.
答案2
得分: 0
感谢您的所有帮助!将所有内容放在一起,我们可以使[a, b, c].kmerge()
正常工作:
use itertools::Itertools;
let a = (0..6).step_by(3);
let b = (1..6).step_by(3);
let c = (2..6).step_by(3);
let it = [a, b, c].kmerge();
itertools::assert_equal(it, [0, 1, 2, 3, 4, 5]);
额外的步骤是扩展所有IntoIterator
的:
use itertools::kmerge;
use itertools::KMerge;
impl<I: IntoIterator> IntoItertoolsPlus for I {}
pub trait IntoItertoolsPlus: IntoIterator {
fn kmerge(self) -> KMerge<<Self::Item as IntoIterator>::IntoIter>
where
Self: IntoIterator + Sized,
Self::Item: IntoIterator,
<<Self as IntoIterator>::Item as IntoIterator>::Item: PartialOrd,
{
kmerge(self)
}
}
我将在我的两个函数中使用这个方法。它们都接受多个相同类型的输入变量。
英文:
Thanks for all the help! Putting it all together, we can make [a, b, c].kmerge()
work:
use itertools::Itertools;
let a = (0..6).step_by(3);
let b = (1..6).step_by(3);
let c = (2..6).step_by(3);
let it = [a, b, c].kmerge();
itertools::assert_equal(it, [0, 1, 2, 3, 4, 5]);
The extra step is to extend all IntoIterator's:
use itertools::kmerge;
use itertools::KMerge;
impl<I: IntoIterator> IntoItertoolsPlus for I {}
pub trait IntoItertoolsPlus: IntoIterator {
fn kmerge(self) -> KMerge<<Self::Item as IntoIterator>::IntoIter>
where
Self: IntoIterator + Sized,
Self::Item: IntoIterator,
<<Self as IntoIterator>::Item as IntoIterator>::Item: PartialOrd,
{
kmerge(self)
}
}
I'm going to use this method for two of my own functions. They both accept a variable number of same-type inputs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论