获取 Scala 2.7.7 中 Regex.MatchIterator 的大小?
在 Scala 2.7.7 中获取 Regex.MatchIterator 的大小最优雅的方法是什么?
我尝试了以下方法:
¤ scala
Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> "a".r.findAllIn("a").size
<console>:5: error: value size is not a member of scala.util.matching.Regex.MatchIterator
"a".r.findAllIn("a").size
^
scala> "a".r.findAllIn("a").size()
<console>:5: error: value size is not a member of scala.util.matching.Regex.MatchIterator
"a".r.findAllIn("a").size()
^
What is the most elegant way to get the size of a Regex.MatchIterator
in Scala 2.7.7?
I tried the following:
¤ scala
Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> "a".r.findAllIn("a").size
<console>:5: error: value size is not a member of scala.util.matching.Regex.MatchIterator
"a".r.findAllIn("a").size
^
scala> "a".r.findAllIn("a").size()
<console>:5: error: value size is not a member of scala.util.matching.Regex.MatchIterator
"a".r.findAllIn("a").size()
^
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以转换迭代器:
请务必保存转换后的迭代器(如果您想在计算大小后访问数据),因为它只能迭代一次。
您还可以使用foldLeft,而不是转换为另一个集合:
You can convert the Iterator:
Be sure to save the converted iterator (if you want to access the data after calculating the size) because it can only iterated once.
Instead of converting to another collection you can also use foldLeft: