Last active
November 20, 2018 06:46
-
-
Save ezura/0cfee146c7f06ace9ec5514f0d6f24e7 to your computer and use it in GitHub Desktop.
Kotlin の Iterator、「次の要素を返す」のと「次の要素があるか」を違うメソッドで管理してるっぽい(´・ω・`)? #CodePiece #speee_lounge #swift #kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # Iterator の実装 | |
// Kotlin | |
// 次の要素があるかどうかは hasNext みる | |
fun next(): T | |
fun hasNext(): Boolean | |
// Swift | |
// 次の要素があるかは next() の返り値の型が Self.Element? なので、これだけで判断可能 | |
func next() -> Self.Element? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin | |
val itr = (1..2).iterator() | |
println(itr.next()) // 1 | |
println(itr.next()) // 2 | |
// !!!: Exception in thread "main" java.util.NoSuchElementException | |
println(itr.next()) | |
// Swift | |
var itr = [1, 2].makeIterator() | |
itr.next() // 1 | |
itr.next() // 2 | |
itr.next() // nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
おそらくですが、Java APIのIteratorから来てるんだと思います―。参考まで。
https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html