Skip to content

Commit

Permalink
Indexed iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
abreslav committed Oct 4, 2012
1 parent 7e36fdf commit 35c8d0e
Showing 1 changed file with 24 additions and 71 deletions.
95 changes: 24 additions & 71 deletions kotlin-examples/src/_02_extensions/MultiDecl.kt
Expand Up @@ -9,7 +9,8 @@ fun main(args : Array<String>) {
val (x, y) = Point(1, 2)
println("($x, $y)")

println(parseFileNameAndLine("Foo12.java:12"))
// testMultiReturn()
// testIndexedIteration()
}

fun Point.component1() = x
Expand Down Expand Up @@ -63,30 +64,28 @@ fun parseFileNameAndLine(str: String): Location {












fun String.toGroupsByRegex(regex: String): List<String>? {
val m = Pattern.compile(regex).matcher(this)
if (!m.matches()) return null
val result = ArrayList<String>()
for (g in 0..m.groupCount()) {
result.add(m.group(g)!!)
fun testIndexedIteration() {
for ((i, c) in "abcd".indexed) {
println("s[$i] = $c")
}
return result
}


val String.indexed: StringIteratorWithIndices
get() = StringIteratorWithIndices(this)

class StringIteratorWithIndices(val str: String) : Iterator<Pair<Int, Char>> {
var i = 0

public override fun next(): Pair<Int, Char> {
val result = Pair(i, str[i])
i++
return result
}

public override fun hasNext(): Boolean = i < str.length

}



Expand All @@ -103,58 +102,12 @@ fun String.toGroupsByRegex(regex: String): List<String>? {











































val String.indexed: StringIteratorWithIndices
get() = StringIteratorWithIndices(this)

class StringIteratorWithIndices(val str: String) : Iterator<Pair<Int, Char>> {
var i = 0

public override fun next(): Pair<Int, Char> {
val result = Pair(i, str[i])
i++
return result
fun String.toGroupsByRegex(regex: String): List<String>? {
val m = Pattern.compile(regex).matcher(this)
if (!m.matches()) return null
val result = ArrayList<String>()
for (g in 0..m.groupCount()) {
result.add(m.group(g)!!)
}

public override fun hasNext(): Boolean = i < str.length

}
return result
}

0 comments on commit 35c8d0e

Please sign in to comment.