Add samples for iterator-related extensions (KT-20357)
This commit is contained in:
committed by
Ilya Gorbunov
parent
94f77c773c
commit
4d13ea89b2
@@ -0,0 +1,72 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package samples.collections
|
||||||
|
|
||||||
|
import samples.Sample
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class Iterators {
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun iteratorForEnumeration() {
|
||||||
|
val vector = Vector<String>().apply {
|
||||||
|
add("RED")
|
||||||
|
add("GREEN")
|
||||||
|
add("BLUE")
|
||||||
|
}
|
||||||
|
|
||||||
|
for (e in vector.elements()) {
|
||||||
|
println("The element is $e")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun iterator() {
|
||||||
|
val mutableList = mutableListOf(1, 2, 3)
|
||||||
|
val mutableIterator = mutableList.iterator()
|
||||||
|
|
||||||
|
if (mutableIterator.hasNext()) {
|
||||||
|
mutableIterator.next()
|
||||||
|
mutableIterator.remove()
|
||||||
|
}
|
||||||
|
|
||||||
|
for (e in mutableIterator) {
|
||||||
|
println("The element is $e")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun withIndexIterator() {
|
||||||
|
val iterator = ('a'..'c').iterator()
|
||||||
|
|
||||||
|
for ((index, value) in iterator.withIndex()) {
|
||||||
|
println("The element at $index is $value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
fun forEachIterator() {
|
||||||
|
val iterator = (1..3).iterator()
|
||||||
|
if (iterator.hasNext()) {
|
||||||
|
iterator.next()
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator.forEach {
|
||||||
|
println("The element is $it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ package kotlin.collections
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an [Iterator] for an [Enumeration], allowing to use it in `for` loops.
|
* Creates an [Iterator] for an [Enumeration], allowing to use it in `for` loops.
|
||||||
|
* @sample samples.collections.Iterators.iteratorForEnumeration
|
||||||
*/
|
*/
|
||||||
@kotlin.jvm.JvmVersion
|
@kotlin.jvm.JvmVersion
|
||||||
public operator fun <T> java.util.Enumeration<T>.iterator(): Iterator<T> = object : Iterator<T> {
|
public operator fun <T> java.util.Enumeration<T>.iterator(): Iterator<T> = object : Iterator<T> {
|
||||||
@@ -15,6 +16,7 @@ public operator fun <T> java.util.Enumeration<T>.iterator(): Iterator<T> = objec
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop.
|
* Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop.
|
||||||
|
* @sample samples.collections.Iterators.iterator
|
||||||
*/
|
*/
|
||||||
@kotlin.internal.InlineOnly
|
@kotlin.internal.InlineOnly
|
||||||
public inline operator fun <T> Iterator<T>.iterator(): Iterator<T> = this
|
public inline operator fun <T> Iterator<T>.iterator(): Iterator<T> = this
|
||||||
@@ -22,11 +24,13 @@ public inline operator fun <T> Iterator<T>.iterator(): Iterator<T> = this
|
|||||||
/**
|
/**
|
||||||
* Returns an [Iterator] wrapping each value produced by this [Iterator] with the [IndexedValue],
|
* Returns an [Iterator] wrapping each value produced by this [Iterator] with the [IndexedValue],
|
||||||
* containing value and it's index.
|
* containing value and it's index.
|
||||||
|
* @sample samples.collections.Iterators.withIndexIterator
|
||||||
*/
|
*/
|
||||||
public fun <T> Iterator<T>.withIndex(): Iterator<IndexedValue<T>> = IndexingIterator(this)
|
public fun <T> Iterator<T>.withIndex(): Iterator<IndexedValue<T>> = IndexingIterator(this)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Performs the given [operation] on each element of this [Iterator].
|
* Performs the given [operation] on each element of this [Iterator].
|
||||||
|
* @sample samples.collections.Iterators.forEachIterator
|
||||||
*/
|
*/
|
||||||
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
|
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
|
||||||
for (element in this) operation(element)
|
for (element in this) operation(element)
|
||||||
|
|||||||
Reference in New Issue
Block a user