-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e9f58a
commit 670e315
Showing
3 changed files
with
119 additions
and
12 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ru.spbstu.wheels | ||
|
||
object Break : NoStackThrowable("break") | ||
object Continue : NoStackThrowable("continue") | ||
|
||
object BreakableContext { | ||
val break_: Nothing get() = throw Break | ||
val continue_: Nothing get() = throw Continue | ||
|
||
inline fun <T> iteration(body: BreakableContext.() -> T): T? = | ||
try { | ||
this.body() | ||
} catch (_: Continue) { | ||
null | ||
} | ||
|
||
inline fun <T> loop(body: BreakableContext.() -> T): T? = | ||
try { | ||
this.body() | ||
} catch (_: Break) { | ||
null | ||
} | ||
} | ||
|
||
inline fun <T> Iterable<T>.forEachB(body: BreakableContext.(T) -> Unit) { | ||
BreakableContext.loop { | ||
forEach { | ||
iteration { body(it) } | ||
} | ||
} | ||
} | ||
|
||
inline fun <T> Sequence<T>.forEachB(body: BreakableContext.(T) -> Unit) { | ||
BreakableContext.loop { | ||
forEach { | ||
iteration { body(it) } | ||
} | ||
} | ||
} | ||
|
||
inline fun <T> Iterable<T>.forEachIndexedB(body: BreakableContext.(Int, T) -> Unit) { | ||
BreakableContext.loop { | ||
forEachIndexed { i, e -> | ||
iteration { body(i, e) } | ||
} | ||
} | ||
} | ||
|
||
inline fun <T> Sequence<T>.forEachIndexedB(body: BreakableContext.(Int, T) -> Unit) { | ||
BreakableContext.loop { | ||
forEachIndexed { i, e -> | ||
iteration { body(i, e) } | ||
} | ||
} | ||
} | ||
|
||
inline fun repeatB(times: Int, body: BreakableContext.(Int) -> Unit) { | ||
BreakableContext.loop { | ||
repeat(times) { | ||
iteration { body(it) } | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ru.spbstu.wheels | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class BreakableTest { | ||
@Test | ||
fun smokeTest() { | ||
|
||
var i = 0 | ||
repeatB(15) { | ||
++i | ||
if (i > 10) break_ | ||
if (i > 0) continue_ | ||
++i | ||
} | ||
assertEquals(11, i) | ||
val mut = mutableListOf<Int>() | ||
(1..300).forEachB { | ||
mut += it | ||
if (it > 200) break_ | ||
} | ||
assertEquals((1..201).toList(), mut) | ||
|
||
} | ||
} |