Skip to content

Commit

Permalink
Breakables
Browse files Browse the repository at this point in the history
  • Loading branch information
belyaev-mikhail committed Sep 28, 2020
1 parent 5e9f58a commit 670e315
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 12 deletions.
42 changes: 30 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toUpperCaseAsciiOnly
import java.net.URI

buildscript {
Expand All @@ -11,7 +13,13 @@ plugins {
`maven-publish`
}

val forceVersion: String? by project
class Props {
fun String.toUpperSnakeCase() = this.replace("([a-z])([A-Z])".toRegex(), "$1_$2").toUpperCaseAsciiOnly()
operator fun getValue(self: Any?, prop: kotlin.reflect.KProperty<*>): String? =
project.findProperty(prop.name)?.toString() ?: System.getenv(prop.name.toUpperSnakeCase())
}

val forceVersion by Props()

project.group = "ru.spbstu"
project.version = forceVersion ?: "0.0.1.0"
Expand All @@ -22,10 +30,22 @@ repositories {
}

kotlin {
jvm()
jvm {
compilations.all {
kotlinOptions.apply {
jvmTarget = "1.6"
}
}
}
js {
nodejs()
browser()
browser {
testTask {
useKarma {
usePhantomJS()
}
}
}
}

sourceSets {
Expand Down Expand Up @@ -76,21 +96,19 @@ kotlin {
}
}

val bintrayOrg: String? by project
val bintrayRepo: String? by project

val bintrayOrg by Props()
val bintrayRepo by Props()
val bintrayUsername by Props()
val bintrayPassword by Props()

publishing {
repositories {
maven {
url = URI("https://api.bintray.com/maven/${bintrayOrg}/${bintrayRepo}/${project.name}/;publish=1;override=1")
credentials {
val bintrayUsername: String? by project
val bintrayPassword: String? by project
val env = System.getenv().withDefault { null }
val BINTRAY_USERNAME by env
val BINTRAY_PASSWORD by env
username = (bintrayUsername ?: BINTRAY_USERNAME)
password = (bintrayPassword ?: BINTRAY_PASSWORD)
username = bintrayUsername
password = bintrayPassword
}
}
}
Expand Down
63 changes: 63 additions & 0 deletions src/commonMain/kotlin/ru/spbstu/wheels/Breakable.kt
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) }
}
}
}
26 changes: 26 additions & 0 deletions src/commonTest/kotlin/ru/spbstu/wheels/BreakableTest.kt
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)

}
}

0 comments on commit 670e315

Please sign in to comment.