gpt4 book ai didi

string - 为什么 Scala 'String' 对象的迭代器和 'List[Int]' 对象的迭代器在这里表现不同?

转载 作者:行者123 更新时间:2023-12-02 06:54:33 25 4
gpt4 key购买 nike

我只是想探索 REPL 中 String 对象和 List[Int] 对象的迭代器的行为,测试如下所示:

scala> val list = List(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

scala> val itL = list.iterator
itL: Iterator[Int] = non-empty iterator

scala> List(8,5,list.size-13).map(itL.take(_).mkString)
res85: List[String] = List(12345678, 910111213, 141516)

scala> val st = "abcdefghijklmnop"
st: String = abcdefghijklmnop

scala> val itS = st.iterator
itS: Iterator[Char] = non-empty iterator

scala> List(8,5,st.size-13).map(itS.take(_).mkString)
res84: List[String] = List(abcdefgh, abcde, abc)

为什么迭代器的行为不同?我在 String 对象的情况下的预期输出是:

List[String] = List(abcdefgh, ijklm, nop)

如果可能的话,有人可以用例子解释一下吗?

另一个观察结果是:Range 对象的迭代器的行为也与 String 对象完全相同如下所示:

scala> val x = (1 to 16)
x: scala.collection.immutable.Range.Inclusive = Range 1 to 16

scala> val t = (1 to 16).iterator
t: Iterator[Int] = non-empty iterator

scala> List(8,5,x.size-13).map(t.take(_).mkString)
res103: List[String] = List(12345678, 12345, 123)

如果 Range 转换为 ListSet,则相应迭代器的行为始终与我的预期完全一致:

scala> val x1 = (1 to 16).toList
x1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

scala> val t1 = x1.iterator
t1: Iterator[Int] = non-empty iterator

scala> List(8,5,x1.size-13).map(t1.take(_).mkString)
res104: List[String] = List(12345678, 910111213, 141516)

scala> val x2 = (1 to 16).toSet
x2: scala.collection.immutable.Set[Int] = Set(5, 10, 14, 1, 6, 9, 13, 2, 12, 7, 3, 16, 11, 8, 4, 15)

scala> val t2 = x2.iterator
t2: Iterator[Int] = non-empty iterator

scala> List(8,5,x2.size-13).map(t2.take(_).mkString)
res105: List[String] = List(51014169132, 12731611, 8415)

最佳答案

Iterator.take(n:Int) API documentation 附加了一条注释:

Reuse: After calling this method, one should discard the iterator it was called on, and use only the iterator that was returned. Using the old iterator is undefined, subject to change, and may result in changes to the new iterator as well.

您似乎发现了一些“未定义”的行为。

关于string - 为什么 Scala 'String' 对象的迭代器和 'List[Int]' 对象的迭代器在这里表现不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51415013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com