一个比”vec![a,b,c].into_iter()”更好的创建临时迭代器的方式是什么?

huangapple go评论94阅读模式
英文:

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:

  1. use itertools::Itertools;
  2. let a = (0..6).step(3);
  3. let b = (1..6).step(3;
  4. let c = (2..6).step(3);
  5. let it = vec![a, b, c].into_iter().kmerge();
  6. 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:

  1. use itertools::Itertools;
  2. let a = (0..6).step(3);
  3. let b = (1..6).step(3);
  4. let c = (2..6).step(3);
  5. let it = vec![a, b, c].into_iter().kmerge();
  6. 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同样有效,避免了堆分配。

  1. [a, b, c].into_iter()

也许文档是在Rust 1.53之前编写的,当时这个功能已经稳定

英文:

An array works just as well as a Vec and avoids the heap allocation.

  1. [a, b, c].into_iter()

Perhaps the documentation predates Rust 1.53 when this was stabilized.

答案2

得分: 0

感谢您的所有帮助!将所有内容放在一起,我们可以使[a, b, c].kmerge()正常工作:

  1. use itertools::Itertools;
  2. let a = (0..6).step_by(3);
  3. let b = (1..6).step_by(3);
  4. let c = (2..6).step_by(3);
  5. let it = [a, b, c].kmerge();
  6. itertools::assert_equal(it, [0, 1, 2, 3, 4, 5]);

额外的步骤是扩展所有IntoIterator的:

  1. use itertools::kmerge;
  2. use itertools::KMerge;
  3. impl<I: IntoIterator> IntoItertoolsPlus for I {}
  4. pub trait IntoItertoolsPlus: IntoIterator {
  5. fn kmerge(self) -> KMerge<<Self::Item as IntoIterator>::IntoIter>
  6. where
  7. Self: IntoIterator + Sized,
  8. Self::Item: IntoIterator,
  9. <<Self as IntoIterator>::Item as IntoIterator>::Item: PartialOrd,
  10. {
  11. kmerge(self)
  12. }
  13. }

我将在我的两个函数中使用这个方法。它们都接受多个相同类型的输入变量。

英文:

Thanks for all the help! Putting it all together, we can make [a, b, c].kmerge() work:

  1. use itertools::Itertools;
  2. let a = (0..6).step_by(3);
  3. let b = (1..6).step_by(3);
  4. let c = (2..6).step_by(3);
  5. let it = [a, b, c].kmerge();
  6. itertools::assert_equal(it, [0, 1, 2, 3, 4, 5]);

The extra step is to extend all IntoIterator's:

  1. use itertools::kmerge;
  2. use itertools::KMerge;
  3. impl&lt;I: IntoIterator&gt; IntoItertoolsPlus for I {}
  4. pub trait IntoItertoolsPlus: IntoIterator {
  5. fn kmerge(self) -&gt; KMerge&lt;&lt;Self::Item as IntoIterator&gt;::IntoIter&gt;
  6. where
  7. Self: IntoIterator + Sized,
  8. Self::Item: IntoIterator,
  9. &lt;&lt;Self as IntoIterator&gt;::Item as IntoIterator&gt;::Item: PartialOrd,
  10. {
  11. kmerge(self)
  12. }
  13. }

I'm going to use this method for two of my own functions. They both accept a variable number of same-type inputs.

huangapple
  • 本文由 发表于 2023年2月16日 09:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定