Update notebooks

This commit is contained in:
Alexander Nozik 2024-12-08 12:38:44 +03:00
parent 5dfd67b563
commit 85b1931673
2 changed files with 269 additions and 263 deletions

View File

@ -138,7 +138,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": "println(\"This is a string with \\$\")", "source": "println(\"This is a string with \\$a\")",
"metadata": { "metadata": {
"datalore": { "datalore": {
"node_id": "hm9iYlWj1B2S0469GtxDrf", "node_id": "hm9iYlWj1B2S0469GtxDrf",
@ -157,13 +157,16 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"println(\"\"\"\n", "println(\n",
" \"\"\"\n",
" \\\\\\\n",
" This is a \n", " This is a \n",
" multi\n", " multi\n",
" line\n", " line\n",
" raw\n", " raw\n",
" string\n", " string\n",
"\"\"\".trimIndent())" "\"\"\".trimIndent()\n",
")"
], ],
"metadata": { "metadata": {
"datalore": { "datalore": {
@ -182,9 +185,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": "println(\"\"\"This is a raw string number ${number + 1} with \\ and ${'$'} \"\"\")",
"println(\"\"\"This is a raw string number ${number+1} with \\ and ${'$'} \"\"\")"
],
"metadata": { "metadata": {
"datalore": { "datalore": {
"node_id": "KDL3D9lOVn6vX3iTAY50gb", "node_id": "KDL3D9lOVn6vX3iTAY50gb",
@ -232,7 +233,7 @@
"source": [ "source": [
"/* Not recommended */\n", "/* Not recommended */\n",
"var counter = 0\n", "var counter = 0\n",
"for(i in 0..20){\n", "for (i in 0..20) {\n",
" counter += i\n", " counter += i\n",
"}\n", "}\n",
"counter" "counter"
@ -277,7 +278,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"val intArray: IntArray = IntArray(21){ it -> it + 1 }\n", "val intArray: IntArray = IntArray(21) { it -> it + 1 }\n",
"intArray.sum()" "intArray.sum()"
], ],
"metadata": { "metadata": {
@ -295,6 +296,20 @@
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
}, },
{
"metadata": {},
"cell_type": "code",
"source": [
"var _a = 2\n",
"val a: Int get() = _a\n",
"\n",
"println(a)\n",
"_a = 3\n",
"println(a)"
],
"outputs": [],
"execution_count": null
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
@ -324,7 +339,7 @@
"/**\n", "/**\n",
" * Functions could be defined everywhere in kotlin code. This is a so-called top-level function.\n", " * Functions could be defined everywhere in kotlin code. This is a so-called top-level function.\n",
" */\n", " */\n",
"fun doSomething(){\n", "fun doSomething() {\n",
" println(\"I did it\")\n", " println(\"I did it\")\n",
"}\n", "}\n",
"\n", "\n",
@ -348,15 +363,15 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"interface AnInterface{\n", "interface AnInterface {\n",
" fun doSomethingInAnObject()\n", " fun doSomethingInAnObject()\n",
"}\n", "}\n",
"\n", "\n",
"object AnObject : AnInterface{\n", "object AnObject : AnInterface {\n",
" /**\n", " /**\n",
" * This is a member-function\n", " * This is a member-function\n",
" */\n", " */\n",
" override fun doSomethingInAnObject(){\n", " override fun doSomethingInAnObject() {\n",
" println(\"I did it in an object\")\n", " println(\"I did it in an object\")\n",
" }\n", " }\n",
"}\n", "}\n",
@ -381,13 +396,14 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"fun doSomethingSpecial(){\n", "fun doSomethingSpecial() {\n",
" val base = \"base\"\n", " val base = \"base\"\n",
" val special = \"special\"\n", " val special = \"special\"\n",
"\n",
" /**\n", " /**\n",
" * This one is inside another function\n", " * This one is inside another function\n",
" */\n", " */\n",
" fun String.doSomethingInside(){\n", " fun String.doSomethingInside() {\n",
" println(\"I did $special inside another function on $this\")\n", " println(\"I did $special inside another function on $this\")\n",
" }\n", " }\n",
" base.doSomethingInside()\n", " base.doSomethingInside()\n",
@ -415,12 +431,12 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"fun returnFunction(): (String) -> Unit {\n", "fun returnFunction(): (String) -> Unit {\n",
" fun printStringWithPrefix(str: String){\n", " fun printStringWithPrefix(str: String) {\n",
" println(\"Prefixed: $str\")\n", " println(\"Prefixed: $str\")\n",
" }\n", " }\n",
" return ::printStringWithPrefix\n", " return ::printStringWithPrefix\n",
" //return { printStringWithPrefix(it) }\n", " //return { printStringWithPrefix(it) }\n",
" // return { println(\"Prefixed: $it\") }\n", " //return { println(\"Prefixed: $it\") }\n",
"}\n", "}\n",
"\n", "\n",
"returnFunction()(\"Haskel, Boo!\")" "returnFunction()(\"Haskel, Boo!\")"
@ -462,7 +478,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"fun returnUnit(): Unit {\n", "fun returnUnit(): Unit {\n",
" // no return statement `Unit` is returned implicitly\n", " // no return statement `Unit` is returned implicitly\n",
" val b = Unit\n", " val b = Unit\n",
@ -487,8 +502,8 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"fun doNothing(){\n", "fun doNothing() {\n",
" \n", "\n",
"}\n", "}\n",
"\n", "\n",
"val a = doNothing()\n", "val a = doNothing()\n",
@ -520,9 +535,10 @@
"}\n", "}\n",
"\n", "\n",
"doSomethingWithDefault()\n", "doSomethingWithDefault()\n",
"doSomethingWithDefault(4)\n",
"doSomethingWithDefault(2, \"aaa\")\n", "doSomethingWithDefault(2, \"aaa\")\n",
"doSomethingWithDefault(b = \"fff\", a = 8)\n", "doSomethingWithDefault(b = \"fff\", a = 8)\n",
"doSomethingWithDefault(4, b = \"fff\")\n", "doSomethingWithDefault(6, b = \"fff\")\n",
"doSomethingWithDefault(a = 2, \"fff\")// don't do that" "doSomethingWithDefault(a = 2, \"fff\")// don't do that"
], ],
"metadata": { "metadata": {
@ -557,7 +573,7 @@
" sum += function(pos)\n", " sum += function(pos)\n",
" pos += step\n", " pos += step\n",
" }\n", " }\n",
" return sum*step\n", " return sum * step\n",
"}\n", "}\n",
"\n", "\n",
"integrate { x ->\n", "integrate { x ->\n",
@ -585,13 +601,13 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"fun functionWithParameters(){\n", "fun functionWithParameters() {\n",
" println(\"without default\")\n", " println(\"without default\")\n",
"}\n", "}\n",
"\n", "\n",
"//@JvmOverloads\n", "//@JvmOverloads\n",
"@JvmName(\"functionWithDefaultParameters\")\n", "@JvmName(\"functionWithDefaultParameters\")\n",
"fun functionWithParameters(a: Int = 2){\n", "fun functionWithParameters(a: Int = 2) { // don't do that\n",
" println(\"with default\")\n", " println(\"with default\")\n",
"}\n", "}\n",
"\n", "\n",
@ -622,7 +638,7 @@
"\n", "\n",
"class Context(val a: String, val b: String)\n", "class Context(val a: String, val b: String)\n",
"\n", "\n",
"fun somethingWith(context: Context) = with(context){\n", "fun somethingWith(context: Context) = with(context) {\n",
" println(a + b)\n", " println(a + b)\n",
"}" "}"
], ],
@ -661,25 +677,25 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"data class BuildResult internal constructor(val str: String, val int: Int, val optional: Double?){\n", "data class BuildResult internal constructor(val str: String, val int: Int, val optional: Double?) {\n",
" init{\n", " init {\n",
" require(optional!= null){ \"null!\" }\n", " require(optional != null) { \"null!\" }\n",
" }\n", " }\n",
"}\n", "}\n",
"\n", "\n",
"class Builder(\n", "class Builder(\n",
" var str: String = \"\", \n", " var str: String = \"\",\n",
" var int: Int = 0\n", " var int: Int = 0\n",
"){\n", ") {\n",
" var optional: Double? = null\n", " var optional: Double? = null\n",
"\n", "\n",
" fun build() = BuildResult(str, int, optional)\n", " fun build() = BuildResult(str, int, optional)\n",
"}\n", "}\n",
"\n", "\n",
"fun BuildResult(defaultInt: Int, block: Builder.() -> Unit): BuildResult = \n", "fun BuildResult(defaultInt: Int, block: Builder.() -> Unit): BuildResult =\n",
" Builder(int = defaultInt).apply(block).build()\n", " Builder(int = defaultInt).apply(block).build()\n",
"\n", "\n",
"val res = BuildResult(2){\n", "val res = BuildResult(2) {\n",
" str = \"ff\"\n", " str = \"ff\"\n",
" optional = 1.0\n", " optional = 1.0\n",
"}\n", "}\n",
@ -717,16 +733,19 @@
"source": [ "source": [
"//Interfaces and objects\n", "//Interfaces and objects\n",
"\n", "\n",
"interface AnInterface {\n", "fun interface AnInterface {\n",
" val a: Int\n", " val a: Int\n",
" get() = 4\n", " get() = 4\n",
" // set(value){\n", "\n",
" // println(value)\n", " // set(value){\n",
" // }\n", " // println(value)\n",
" // }\n",
" fun doSomething() //= println(\"From interface\")\n", " fun doSomething() //= println(\"From interface\")\n",
"\n",
" fun doSomething2() = Unit\n",
"}\n", "}\n",
"\n", "\n",
"abstract class AnAbstractClass(override val a: Int): AnInterface{\n", "abstract class AnAbstractClass(override val a: Int) : AnInterface {\n",
" override final fun doSomething() = println(\"From a class\")\n", " override final fun doSomething() = println(\"From a class\")\n",
" abstract fun doSomethingElse()\n", " abstract fun doSomethingElse()\n",
"}\n", "}\n",
@ -747,25 +766,30 @@
"}\n", "}\n",
"\n", "\n",
"/**\n", "/**\n",
"* Creating an instance\n", " * Creating an instance\n",
"*/\n", " */\n",
"val instance = AClass(3)\n", "val instance = AClass(3)\n",
"\n", "\n",
"/**\n", "/**\n",
"* Using singleton reference without constructor invocation\n", " * Using singleton reference without constructor invocation\n",
"*/\n", " */\n",
"val obj: AnInterface = AnObject\n", "val obj: AnInterface = AnObject\n",
"\n", "\n",
"/**\n", "/**\n",
"* Anonymous object\n", " * Anonymous object\n",
"*/\n", " */\n",
"val anonymous = object : AnInterface {\n", "val anonymous = object : AnInterface {\n",
" override fun doSomething(): Unit = TODO(\"Not yet implemented\")\n", " override fun doSomething(): Unit = TODO(\"Not yet implemented\")\n",
" override fun doSomething2(): Unit = TODO(\"Not yet implemented\")\n",
"}\n",
"\n",
"val anonymous2 = AnInterface {\n",
" println(\"Do something\")\n",
"}\n", "}\n",
"\n", "\n",
"/**\n", "/**\n",
"* The one that should not be named\n", " * The one that should not be named\n",
"*/\n", " */\n",
"val voldemort = object {\n", "val voldemort = object {\n",
" fun doSomething(): Unit = TODO()\n", " fun doSomething(): Unit = TODO()\n",
"}\n", "}\n",
@ -787,23 +811,23 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"fun interface Float64Function: (Double) -> Double {\n", "fun interface Float64Function : (Double) -> Double {\n",
" override operator fun invoke(arg: Double): Double\n", " override operator fun invoke(arg: Double): Double\n",
" \n", "\n",
" operator fun invoke(doubles: DoubleArray): DoubleArray = DoubleArray(doubles.size){ invoke( doubles[it]) }\n", " operator fun invoke(doubles: DoubleArray): DoubleArray = DoubleArray(doubles.size) { invoke(doubles[it]) }\n",
"}\n", "}\n",
"\n", "\n",
"val sin = object : Float64Function{\n", "val sin = object : Float64Function {\n",
" override fun invoke(p1: Double): Double = sin(p1)\n", " override fun invoke(p1: Double): Double = sin(p1)\n",
"}\n", "}\n",
"\n", "\n",
"val cos = Float64Function{ kotlin.math.cos(it) }\n", "val cos = Float64Function { kotlin.math.cos(it) }\n",
"\n", "\n",
"val tg = Float64Function(::tan)\n", "val tg = Float64Function(::tan)\n",
"\n", "\n",
"sin(PI/2)\n", "sin(PI / 2)\n",
"\n", "\n",
"sin(doubleArrayOf(0.0, PI/2))" "sin(doubleArrayOf(0.0, PI / 2))"
], ],
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
@ -828,15 +852,15 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"interface Producer<out T>{\n", "interface Producer<out T> {\n",
" fun produce(): T\n", " fun produce(): T\n",
"}\n", "}\n",
"\n", "\n",
"interface Consumer<in T>{\n", "interface Consumer<in T> {\n",
" fun consume(value: T)\n", " fun consume(value: T)\n",
"}\n", "}\n",
"\n", "\n",
"interface Doer<T>: Producer<T>, Consumer<T> {\n", "interface Doer<T> : Producer<T>, Consumer<T> {\n",
"\n", "\n",
"}" "}"
], ],
@ -924,7 +948,7 @@
" is String -> println(\"I am a String. Length is ${arg.length}\")\n", " is String -> println(\"I am a String. Length is ${arg.length}\")\n",
" is Int -> println(\"I am an Int.\")\n", " is Int -> println(\"I am an Int.\")\n",
" is Double -> println(\"I am a Double\")\n", " is Double -> println(\"I am a Double\")\n",
" // 2==2 -> println(\"Wat?\")\n", " //2==2 -> println(\"Wat?\")\n",
" else -> println(\"I don't know who am I?\")\n", " else -> println(\"I don't know who am I?\")\n",
"}\n", "}\n",
"\n", "\n",
@ -994,8 +1018,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n", "val res: Result<Int> = runCatching<Int> {\n",
"val res: Result<Int> = runCatching<Int> { \n",
" //error(\"Error happened\")\n", " //error(\"Error happened\")\n",
" 4\n", " 4\n",
"}\n", "}\n",
@ -1098,6 +1121,19 @@
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
}, },
{
"metadata": {},
"cell_type": "code",
"source": [
"fun loop(block: () -> Unit): Nothing {\n",
" while (true) {\n",
" block()\n",
" }\n",
"}"
],
"outputs": [],
"execution_count": null
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
@ -1116,13 +1152,16 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n", "class SimpleClass(val a: Int, val b: Double, c: Double) {\n",
"class SimpleClass(val a: Int, val b: Double, c: Double){\n",
" override fun toString() = \"SimpleClass(a=$a, b=$b)\"\n", " override fun toString() = \"SimpleClass(a=$a, b=$b)\"\n",
"}\n", "}\n",
"\n", "\n",
"\n", "\n",
"data class DataClass(val a: Int, var b: Double /*, d: Double */) {\n", "data class DataClass(val a: Int, var b: Double /*, d: Double */) {\n",
" init {\n",
" require(b >= 0) { \"B should be positive\" }\n",
" }\n",
"\n",
" val c get() = b + 1\n", " val c get() = b + 1\n",
"}\n", "}\n",
"\n", "\n",
@ -1205,9 +1244,9 @@
"import java.util.Optional\n", "import java.util.Optional\n",
"import kotlin.reflect.typeOf\n", "import kotlin.reflect.typeOf\n",
"\n", "\n",
"println(typeOf<Optional<Boolean>>() == typeOf<Optional<Optional<Boolean>>>())\n", "println(typeOf<Optional<Boolean>>() == typeOf<Optional<Optional<Boolean>>>())\n",
"\n", "\n",
"println( typeOf<Boolean?>() == typeOf<Boolean??>()) " "println(typeOf<Boolean?>() == typeOf<Boolean??>())"
], ],
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
@ -1215,7 +1254,7 @@
{ {
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": "mapOf<String,String>().get(\"f\")!!", "source": "mapOf<String, String>().get(\"f\")!!",
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
}, },
@ -1237,6 +1276,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"import java.io.File\n",
"\n", "\n",
"/**\n", "/**\n",
" * Safe call and elvis operator\n", " * Safe call and elvis operator\n",
@ -1311,7 +1351,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"/**\n", "/**\n",
" * Dart-like (?=) nullable assignment\n", " * Dart-like (?=) nullable assignment\n",
" */\n", " */\n",
@ -1382,10 +1421,10 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"infix fun <T: Comparable<T>> ClosedRange<T>.intersect(other: ClosedRange<T>): ClosedRange<T>?{\n", "infix fun <T : Comparable<T>> ClosedRange<T>.intersect(other: ClosedRange<T>): ClosedRange<T>? {\n",
" val start = maxOf(this.start, other.start)\n", " val start = maxOf(this.start, other.start)\n",
" val end = minOf(this.endInclusive, other.endInclusive)\n", " val end = minOf(this.endInclusive, other.endInclusive)\n",
" return if(end>=start) start..end else null\n", " return if (end >= start) start..end else null\n",
"}\n", "}\n",
"\n", "\n",
"(0..8).intersect(6..12)\n", "(0..8).intersect(6..12)\n",
@ -1405,7 +1444,7 @@
{ {
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": "", "source": "0.0..8.0 intersect 2.9..6.1",
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
}, },
@ -1413,8 +1452,8 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"fun List<String>.concat() = joinToString(separator = \"\")\n", "fun List<String>.concat() = joinToString(separator = \"\")\n",
"listOf(\"a\",\"b\",\"c\").concat()\n", "listOf(\"a\", \"b\", \"c\").concat()\n",
"listOf(1,2,3).concat()" "listOf(1, 2, 3).concat()"
], ],
"metadata": { "metadata": {
"datalore": { "datalore": {
@ -1438,7 +1477,7 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"val functionWithReciever: List<String>.(arg: Int) -> Unit = {arg->\n", "val functionWithReciever: List<String>.(arg: Int) -> Unit = { arg ->\n",
" println(get(arg))\n", " println(get(arg))\n",
"}\n", "}\n",
"\n", "\n",
@ -1467,13 +1506,12 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"/**\n", "/**\n",
" * Extension property (must be virtual)\n", " * Extension property (must be virtual)\n",
" */\n", " */\n",
"val List<Number>.odd get() = filter { it.toInt() % 2 == 1 }\n", "val List<Number>.odd get() = filter { it.toInt() % 2 == 1 }\n",
"\n", "\n",
"List(10){it}.odd" "List(10) { it }.odd"
], ],
"metadata": { "metadata": {
"datalore": { "datalore": {
@ -1491,15 +1529,22 @@
"source": [ "source": [
"var MutableMap<String, String>.a: String?\n", "var MutableMap<String, String>.a: String?\n",
" get() = this.get(\"a\")\n", " get() = this.get(\"a\")\n",
" set(value){\n", " set(value) {\n",
" if(value == null){\n", " if (value == null) {\n",
" this.remove(\"a\")\n", " this.remove(\"a\")\n",
" } else {\n", " } else {\n",
" this.set(\"a\",value)\n", " this.set(\"a\", value)\n",
" }\n", " }\n",
" }\n", " }\n",
"\n", "\n",
"val map = mutableMapOf(\"a\" to \"a\", \"b\" to \"b\")\n", "val map = mutableMapOf(\n",
" \"a\" to \"a\",\n",
" \"b\" to \"b\"\n",
")\n",
"val map2 = mutableMapOf(\n",
" Pair(\"a\", \"a\"),\n",
" Pair(\"b\", \"b\")\n",
")\n",
"\n", "\n",
"map.a = \"6\"\n", "map.a = \"6\"\n",
"\n", "\n",
@ -1519,7 +1564,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"/**\n", "/**\n",
" * Extension variable (also must be virtual)\n", " * Extension variable (also must be virtual)\n",
" */\n", " */\n",
@ -1530,7 +1574,7 @@
" }\n", " }\n",
"\n", "\n",
"\n", "\n",
"val array = Array(5){it}\n", "val array = Array(5) { it }\n",
"array.second = 9\n", "array.second = 9\n",
"array" "array"
], ],
@ -1563,7 +1607,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"object AClass{\n", "object AClass {\n",
" val a = \"a\"\n", " val a = \"a\"\n",
" val b = \"b\"\n", " val b = \"b\"\n",
" val c = \"c\"\n", " val c = \"c\"\n",
@ -1580,7 +1624,7 @@
"println(\"a = ${AClass.a}, b = ${AClass.b}, c = ${AClass.c}\")\n", "println(\"a = ${AClass.a}, b = ${AClass.b}, c = ${AClass.c}\")\n",
"\n", "\n",
"// Using `with`\n", "// Using `with`\n",
"val res = with(AClass){\n", "val res = with(AClass) {\n",
" // AClass type is the receiver in this scope\n", " // AClass type is the receiver in this scope\n",
" println(\"a = ${this.a}, b = $b, c = $c\")\n", " println(\"a = ${this.a}, b = $b, c = $c\")\n",
" /*return@with*/ \"some value\"\n", " /*return@with*/ \"some value\"\n",
@ -1603,11 +1647,11 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"object AContext{\n", "object AContext {\n",
" fun AClass.abc() = a + b + c // warning additional concatenation\n", " fun AClass.abc() = a + b + c // warning additional concatenation\n",
"}\n", "}\n",
"\n", "\n",
"fun printAbc(aClass: AClass) = with(AContext){\n", "fun printAbc(aClass: AClass) = with(AContext) {\n",
" aClass.abc()\n", " aClass.abc()\n",
"}" "}"
], ],
@ -1617,7 +1661,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"//using `run`\n", "//using `run`\n",
"getAClass()?.takeIf { it.a.isNotEmpty() }?.run {\n", "getAClass()?.takeIf { it.a.isNotEmpty() }?.run {\n",
" // the same as above\n", " // the same as above\n",
@ -1626,7 +1669,7 @@
"}\n", "}\n",
"\n", "\n",
"val runResult: Int = run {\n", "val runResult: Int = run {\n",
" \n", "\n",
"}\n", "}\n",
"\n", "\n",
"//Using `let` to compose result. Not recommended using without a need\n", "//Using `let` to compose result. Not recommended using without a need\n",
@ -1650,7 +1693,15 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"//Don't do that\n", "//Don't do that\n",
"fun scopeAbuse(str: String?) = str.takeIf { it?.isNotEmpty() == true }?.let { it.substring(0..4) }?.let { it.matches(\".*\".toRegex())}" "fun scopeAbuse(str: String?) =\n",
" str.takeIf { it?.isNotEmpty() == true }?.let { it.substring(0..4) }?.let { it.matches(\".*\".toRegex()) }\n",
"\n",
"fun withoutAbuse(str: String?): Boolean? = if (str?.isNotEmpty() == true) {\n",
" val substring = str.substring(0..4)\n",
" substring.matches(\".*\".toRegex())\n",
"} else {\n",
" null\n",
"}"
], ],
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
@ -1679,10 +1730,14 @@
"var i = 2\n", "var i = 2\n",
"\n", "\n",
"/**\n", "/**\n",
"* [also] block does not affect the result\n", " * [also] block does not affect the result\n",
"*/\n", " */\n",
"fun getAndIncrement() = i.also { i += 1 } //don't do that\n", "fun getAndIncrement() = i.also { i += 1 } //don't do that\n",
"\n", "\n",
"fun incrementAndPring(arg: Int) = (arg + 1).also{\n",
" println(it)\n",
"}\n",
"\n",
"println(getAndIncrement())\n", "println(getAndIncrement())\n",
"println(i)" "println(i)"
], ],
@ -1700,7 +1755,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"class Rectangle {\n", "class Rectangle {\n",
" var length: Number = 0\n", " var length: Number = 0\n",
" var breadth: Number = 0\n", " var breadth: Number = 0\n",
@ -1708,9 +1762,9 @@
"}\n", "}\n",
"\n", "\n",
"/**\n", "/**\n",
" * Configure properties of an object (apply)\n", " * Configure properties of an object (apply)\n",
" * https://kotlinlang.org/docs/idioms.html#configure-properties-of-an-object-apply\n", " * https://kotlinlang.org/docs/idioms.html#configure-properties-of-an-object-apply\n",
" */\n", " */\n",
"val myRectangle = Rectangle().apply {\n", "val myRectangle = Rectangle().apply {\n",
" length = 4\n", " length = 4\n",
" breadth = 5\n", " breadth = 5\n",
@ -1740,6 +1794,13 @@
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
}, },
{
"metadata": {},
"cell_type": "code",
"source": "",
"outputs": [],
"execution_count": null
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": [
@ -1790,10 +1851,9 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"/**\n", "/**\n",
"* This one creates a mutable list\n", " * This one creates a mutable list\n",
"*/\n", " */\n",
"val mutableList: MutableList<String> = mutableListOf(\"a\", \"b\", \"c\")\n", "val mutableList: MutableList<String> = mutableListOf(\"a\", \"b\", \"c\")\n",
"mutableList[2] = \"d\"\n", "mutableList[2] = \"d\"\n",
"mutableList.add(\"e\")\n", "mutableList.add(\"e\")\n",
@ -1815,7 +1875,6 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"//don't do that ever\n", "//don't do that ever\n",
"fun doBadThingWithList(list: List<String>): List<String> = (list as MutableList<String>).apply { add(\"something\") }\n", "fun doBadThingWithList(list: List<String>): List<String> = (list as MutableList<String>).apply { add(\"something\") }\n",
"\n", "\n",
@ -1844,9 +1903,9 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"val mutableList = mutableListOf(1,2,3)\n", "val mutableList = mutableListOf(1, 2, 3)\n",
"\n", "\n",
"fun consumeList(list: List<Int>){\n", "fun consumeList(list: List<Int>) {\n",
" println(list.joinToString())\n", " println(list.joinToString())\n",
"}\n", "}\n",
"consumeList(mutableList)\n", "consumeList(mutableList)\n",
@ -1859,7 +1918,7 @@
{ {
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": "listOf(1,2) + listOf(3,4)", "source": "listOf(1, 2) + listOf(3, 4)",
"outputs": [], "outputs": [],
"execution_count": null "execution_count": null
}, },
@ -1895,7 +1954,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"/**\n", "/**\n",
" * This one creates a mutable ArrayList.\n", " * This one creates a mutable ArrayList.\n",
" */\n", " */\n",
@ -1923,10 +1981,9 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"//Bonus\n", "//Bonus\n",
"\n", "\n",
"val lambdaList = List(3){ it.toString() }\n", "val lambdaList = List(3) { it.toString() }\n",
"println(lambdaList)\n", "println(lambdaList)\n",
"\n", "\n",
"val builderList: List<Int> = buildList {\n", "val builderList: List<Int> = buildList {\n",
@ -1934,6 +1991,7 @@
" add(8)\n", " add(8)\n",
" remove(8)\n", " remove(8)\n",
"}\n", "}\n",
"\n",
"builderList" "builderList"
], ],
"metadata": { "metadata": {
@ -1985,11 +2043,11 @@
"/**\n", "/**\n",
" * Use shortcut function to provide default values\n", " * Use shortcut function to provide default values\n",
" */\n", " */\n",
"fun doSomething(additionalArguments: List<String> = emptyList()){\n", "fun doSomething(additionalArguments: List<String> = emptyList()) {\n",
" TODO()\n", " TODO()\n",
" emptyArray<String>()\n", " emptyArray<String>()\n",
" emptySet<String>()\n", " emptySet<String>()\n",
" emptyMap<String,String>()\n", " emptyMap<String, String>()\n",
"}\n", "}\n",
"emptyList<String>()::class" "emptyList<String>()::class"
], ],
@ -2055,14 +2113,13 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"//val entry: MutableMap.MutableEntry<String, String> = map.iterator().next()\n", "//val entry: MutableMap.MutableEntry<String, String> = map.iterator().next()\n",
"\n", "\n",
"//map.entries.first().component2()\n", "//map.entries.first().component2()\n",
"\n", "\n",
"/**\n", "/**\n",
" * The destructuring declaration for maps and other objects that support `operator fun componentN`\n", " * The destructuring declaration for maps and other objects that support `operator fun componentN`\n",
" */\n", " */\n",
"for ((k: String, v) in map) {\n", "for ((k: String, v) in map) {\n",
"//val (k, v) = entry\n", "//val (k, v) = entry\n",
"// val k = entry.component1()\n", "// val k = entry.component1()\n",
@ -2083,7 +2140,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": "map.forEach { (k, v) -> println(\"$k -> $v\")}", "source": [
"map.forEach { (k, v) -> println(\"$k -> $v\") }\n",
"\n",
"// java version\n",
"map.forEach { k, v -> println(\"$k -> $v\") }"
],
"metadata": { "metadata": {
"datalore": { "datalore": {
"node_id": "s1Q65aNWb9YNBZOgxmjotW", "node_id": "s1Q65aNWb9YNBZOgxmjotW",
@ -2098,12 +2160,11 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"val (a, b) = Pair(1, 2)\n", "val (a, b) = Pair(1, 2)\n",
"\n", "\n",
"val coord = doubleArrayOf(0.0, 1.0, 2.0)\n", "val coord = doubleArrayOf(0.0, 1.0, 2.0)\n",
"\n", "\n",
"val (x,y,z) = coord\n", "val (x, y, z) = coord\n",
"\n", "\n",
"data class Coordinate(val x: Double, val y: Int)\n", "data class Coordinate(val x: Double, val y: Int)\n",
"\n", "\n",
@ -2140,25 +2201,24 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"class AClassWithList{\n", "class AClassWithList {\n",
" var b: Double = 2.0\n", " var b: Double = 2.0\n",
" private set\n", " private set\n",
"\n", "\n",
" init{\n", " init {\n",
" b = 5.0\n", " b = 5.0\n",
" }\n", " }\n",
"\n", "\n",
" private val _list: MutableList<Int> = ArrayList<Int>()\n", " private val _list: MutableList<Int> = ArrayList<Int>()\n",
" val list: List<Int> get() = _list\n", " val list: List<Int> get() = _list\n",
" \n", "\n",
" fun add(int: Int){\n", " fun add(int: Int) {\n",
" require(int>0)\n", " require(int > 0)\n",
" _list.add(int)\n", " _list.add(int)\n",
" }\n", " }\n",
"}\n", "}\n",
"\n", "\n",
"\n", "\n",
"\n",
"val obj = AClassWithList()\n", "val obj = AClassWithList()\n",
"\n", "\n",
"// obj.b = 10.0 //error\n", "// obj.b = 10.0 //error\n",
@ -2178,9 +2238,7 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [ "source": "## Wrap mutable logic / Kotlin builder pattern",
"## Wrap mutable logic"
],
"attachments": {}, "attachments": {},
"metadata": { "metadata": {
"datalore": { "datalore": {
@ -2195,7 +2253,7 @@
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"val list = buildList {\n", "val list = buildList {\n",
" repeat(10){\n", " repeat(10) {\n",
" add(it)\n", " add(it)\n",
" }\n", " }\n",
"}\n", "}\n",
@ -2222,11 +2280,11 @@
" var a: String = a\n", " var a: String = a\n",
" var b: String = \"\"\n", " var b: String = \"\"\n",
" var c = 0\n", " var c = 0\n",
" \n", "\n",
" fun build() = ImmutableObject(a, b, c)\n", " fun build() = ImmutableObject(a, b, c)\n",
"}\n", "}\n",
"\n", "\n",
"fun ImmutableObject(a: String, block: ImmutableObjectBuilder.() -> Unit): ImmutableObject =\n", "/*inline*/ fun ImmutableObject(a: String, block: ImmutableObjectBuilder.() -> Unit): ImmutableObject =\n",
" ImmutableObjectBuilder(a).apply(block).build()\n", " ImmutableObjectBuilder(a).apply(block).build()\n",
"\n", "\n",
"ImmutableObject(\"aValue\") {\n", "ImmutableObject(\"aValue\") {\n",
@ -2259,7 +2317,6 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"val emailsList = emptyList<String>()\n", "val emailsList = emptyList<String>()\n",
"\n", "\n",
"// When used directly infix in operator checks if the element is contained in a collection\n", "// When used directly infix in operator checks if the element is contained in a collection\n",
@ -2310,7 +2367,6 @@
}, },
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"// Another (different) use of `in` is iteration over range or collection using\n", "// Another (different) use of `in` is iteration over range or collection using\n",
"// using `operator fun iterator`\n", "// using `operator fun iterator`\n",
"\n", "\n",
@ -2322,7 +2378,7 @@
" println(i)\n", " println(i)\n",
"} //the same, but with boxing\n", "} //the same, but with boxing\n",
"\n", "\n",
"for (i in 1..<100) {\n", "for (i in 1 ..< 100) {\n",
" println(i)\n", " println(i)\n",
"} // half-open range: does not include 100\n", "} // half-open range: does not include 100\n",
"\n", "\n",
@ -2345,7 +2401,7 @@
" }\n", " }\n",
"}\n", "}\n",
"\n", "\n",
"for (x in 0.0..10.0 step 0.5){\n", "for (x in 0.0..10.0 step 0.5) {\n",
" println(x)\n", " println(x)\n",
"}" "}"
], ],
@ -2385,6 +2441,7 @@
" //.onEach { println(it) }\n", " //.onEach { println(it) }\n",
" //.sumOf { it } //use one of reduce operations\n", " //.sumOf { it } //use one of reduce operations\n",
" .reduce { acc: Int, i: Int -> acc + i }\n", " .reduce { acc: Int, i: Int -> acc + i }\n",
" //.fold(0.0) { acc: Double, i: Int -> acc + i }\n",
"\n", "\n",
"result" "result"
], ],
@ -2404,7 +2461,7 @@
"source": [ "source": [
"val sequence = sequence {\n", "val sequence = sequence {\n",
" var counter = 1\n", " var counter = 1\n",
" while(true){\n", " while (true) {\n",
" yield(counter++)\n", " yield(counter++)\n",
" yield(counter++)\n", " yield(counter++)\n",
" // println(counter)\n", " // println(counter)\n",
@ -2485,7 +2542,7 @@
"}\n", "}\n",
"\n", "\n",
"data class IntContainer(val arg: Int) {\n", "data class IntContainer(val arg: Int) {\n",
" \n", "\n",
" companion object : Factory<IntContainer> {\n", " companion object : Factory<IntContainer> {\n",
" override fun build(str: String) = IntContainer(str.toInt())\n", " override fun build(str: String) = IntContainer(str.toInt())\n",
"\n", "\n",
@ -2494,7 +2551,7 @@
"}\n", "}\n",
"\n", "\n",
"data class DoubleContainer(val arg: Double) {\n", "data class DoubleContainer(val arg: Double) {\n",
" \n", "\n",
" companion object : Factory<DoubleContainer> {\n", " companion object : Factory<DoubleContainer> {\n",
" override fun build(str: String) = DoubleContainer(str.toDouble())\n", " override fun build(str: String) = DoubleContainer(str.toDouble())\n",
" }\n", " }\n",
@ -2534,8 +2591,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n", "open class Bad {\n",
"open class Bad{\n",
" val value: Int = requestValue()\n", " val value: Int = requestValue()\n",
"\n", "\n",
" open fun requestValue(): Int {\n", " open fun requestValue(): Int {\n",
@ -2543,7 +2599,7 @@
" return 2\n", " return 2\n",
" }\n", " }\n",
"\n", "\n",
" private fun doSomethingElse(){\n", " private fun doSomethingElse() {\n",
" println(value)\n", " println(value)\n",
" }\n", " }\n",
"}\n", "}\n",
@ -2565,7 +2621,7 @@
"metadata": {}, "metadata": {},
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"class BadString{\n", "class BadString {\n",
" val value: String = requestValue()\n", " val value: String = requestValue()\n",
"\n", "\n",
" fun requestValue(): String {\n", " fun requestValue(): String {\n",
@ -2573,7 +2629,7 @@
" return \"2\"\n", " return \"2\"\n",
" }\n", " }\n",
"\n", "\n",
" private fun doSomethingElse(){\n", " private fun doSomethingElse() {\n",
" println(value)\n", " println(value)\n",
" }\n", " }\n",
"}\n", "}\n",
@ -2586,11 +2642,9 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"\n",
"//Factory functions are preferred to the initialization logic\n", "//Factory functions are preferred to the initialization logic\n",
"\n", "\n",
"data class Good internal constructor(val value: Int){\n", "data class Good internal constructor(val value: Int) {\n",
" init {\n", " init {\n",
" //Initialization block is there to check arguments\n", " //Initialization block is there to check arguments\n",
" require(value >= 0)\n", " require(value >= 0)\n",
@ -2599,11 +2653,10 @@
" companion object\n", " companion object\n",
"}\n", "}\n",
"\n", "\n",
"fun requestValue(): Int = TODO()\n", "private fun requestValue(): Int = TODO()\n",
"\n", "\n",
"// This is the factory-function\n", "// This is the factory-function\n",
"fun Good() = Good(requestValue())\n", "fun Good(): Good = Good(requestValue())\n",
"\n",
"\n", "\n",
"// additional constructor-like builders could be added to the companion\n", "// additional constructor-like builders could be added to the companion\n",
"\n", "\n",
@ -2641,7 +2694,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"class ClassWithALazyProperty{\n", "class ClassWithALazyProperty {\n",
" //Use lazy delegate for something that should be calculated ones on first call\n", " //Use lazy delegate for something that should be calculated ones on first call\n",
" val lazyValue by lazy {\n", " val lazyValue by lazy {\n",
" //Do dome heavy logic here\n", " //Do dome heavy logic here\n",
@ -2649,16 +2702,18 @@
" 22\n", " 22\n",
" }\n", " }\n",
"\n", "\n",
" val getterValue: Int get(){\n", " val getterValue: Int\n",
" println(\"got\")\n", " get() {\n",
" return 33\n", " println(\"got\")\n",
" }\n", " return 33\n",
" }\n",
"}\n", "}\n",
"\n",
"val lazyClass = ClassWithALazyProperty()\n", "val lazyClass = ClassWithALazyProperty()\n",
"lazyClass.lazyValue\n", "println(lazyClass.lazyValue)\n",
"lazyClass.lazyValue\n", "println(lazyClass.lazyValue)\n",
"lazyClass.getterValue\n", "println(lazyClass.getterValue)\n",
"lazyClass.getterValue" "println(lazyClass.getterValue)"
], ],
"metadata": { "metadata": {
"datalore": { "datalore": {
@ -2674,7 +2729,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"//Using other delegates\n", "//Using other delegates\n",
"val map = mutableMapOf(\"a\" to 1, \"b\" to 2)\n", "val map = mutableMapOf(\"a\" to 1, \"b\" to 2)\n",
"\n", "\n",
@ -2713,7 +2767,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"/**\n", "/**\n",
" * Definition of inline function\n", " * Definition of inline function\n",
" */\n", " */\n",
@ -2749,7 +2802,6 @@
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "source": [
"\n",
"/**\n", "/**\n",
" * Using inline function for type reification during the compile time\n", " * Using inline function for type reification during the compile time\n",
" */\n", " */\n",

View File

@ -2,7 +2,6 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -11,10 +10,11 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"%use coroutines" "%use coroutines"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -40,7 +40,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -49,16 +48,16 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"repeat(10){\n", "repeat(10){\n",
" println(\"Line number $it\")\n", " println(\"Line number $it\")\n",
"}" "}"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -67,12 +66,13 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"import java.util.stream.*\n", "import java.util.stream.*\n",
"\n", "\n",
"IntStream.range(0, 100).sum()" "IntStream.range(0, 100).sum()"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -91,7 +91,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -100,64 +99,18 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"null\n",
"java.util.ConcurrentModificationException\n",
"\tat java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1095)\n",
"\tat java.base/java.util.ArrayList$Itr.next(ArrayList.java:1049)\n",
"\tat Line_0_jupyter.<init>(Line_0.jupyter.kts:6)\n",
"\tat java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)\n",
"\tat java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)\n",
"\tat java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)\n",
"\tat kotlin.script.experimental.jvm.BasicJvmScriptEvaluator.evalWithConfigAndOtherScriptsResults(BasicJvmScriptEvaluator.kt:122)\n",
"\tat kotlin.script.experimental.jvm.BasicJvmScriptEvaluator.invoke$suspendImpl(BasicJvmScriptEvaluator.kt:48)\n",
"\tat kotlin.script.experimental.jvm.BasicJvmScriptEvaluator.invoke(BasicJvmScriptEvaluator.kt)\n",
"\tat kotlin.script.experimental.jvm.BasicJvmReplEvaluator.eval(BasicJvmReplEvaluator.kt:49)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.InternalEvaluatorImpl$eval$resultWithDiagnostics$1.invokeSuspend(InternalEvaluatorImpl.kt:127)\n",
"\tat kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)\n",
"\tat kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)\n",
"\tat kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:277)\n",
"\tat kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:95)\n",
"\tat kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:69)\n",
"\tat kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)\n",
"\tat kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:48)\n",
"\tat kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.InternalEvaluatorImpl.eval(InternalEvaluatorImpl.kt:127)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$execute$1$result$1.invoke(CellExecutorImpl.kt:79)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl$execute$1$result$1.invoke(CellExecutorImpl.kt:77)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withHost(ReplForJupyterImpl.kt:758)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.CellExecutorImpl.execute(CellExecutorImpl.kt:77)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.execution.CellExecutor$DefaultImpls.execute$default(CellExecutor.kt:12)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evaluateUserCode(ReplForJupyterImpl.kt:581)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.access$evaluateUserCode(ReplForJupyterImpl.kt:136)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl$evalEx$1.invoke(ReplForJupyterImpl.kt:439)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl$evalEx$1.invoke(ReplForJupyterImpl.kt:436)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.withEvalContext(ReplForJupyterImpl.kt:417)\n",
"\tat org.jetbrains.kotlinx.jupyter.repl.impl.ReplForJupyterImpl.evalEx(ReplForJupyterImpl.kt:436)\n",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor$processExecuteRequest$1$response$1$1.invoke(IdeCompatibleMessageRequestProcessor.kt:140)\n",
"\tat org.jetbrains.kotlinx.jupyter.messaging.IdeCompatibleMessageRequestProcessor$processExecuteRequest$1$response$1$1.invoke(IdeCompatibleMessageRequestProcessor.kt:139)\n",
"\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$Task.execute(JupyterExecutorImpl.kt:42)\n",
"\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$executorThread$1.invoke(JupyterExecutorImpl.kt:82)\n",
"\tat org.jetbrains.kotlinx.jupyter.execution.JupyterExecutorImpl$executorThread$1.invoke(JupyterExecutorImpl.kt:80)\n",
"\tat kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)\n"
]
}
],
"source": [ "source": [
"val list = (0..100).toMutableList()\n", "val list = (0..100).toMutableList()\n",
"\n", "\n",
"list.forEach {\n", "list.forEach {\n",
" if(it % 2 == 0) list.remove(it)\n", " if(it % 2 == 0) list.remove(it)\n",
"}" "}"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -166,8 +119,9 @@
"type": "CODE" "type": "CODE"
} }
}, },
"source": [],
"outputs": [], "outputs": [],
"source": [] "execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -186,7 +140,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -195,16 +148,16 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"val sum = IntStream.range(0, 100).parallel().sum()\n", "val sum = IntStream.range(0, 100).parallel().sum()\n",
"\n", "\n",
"println(sum)" "println(sum)"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -213,8 +166,9 @@
"type": "CODE" "type": "CODE"
} }
}, },
"source": [],
"outputs": [], "outputs": [],
"source": [] "execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -233,7 +187,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -242,18 +195,18 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"fun runLongTask(result: Int = 8): Int {\n", "fun runLongTask(result: Int = 8): Int {\n",
" Thread.sleep(100)\n", " Thread.sleep(100)\n",
" println(\"Task complete: $result\")\n", " println(\"Task complete: $result\")\n",
" return result\n", " return result\n",
"}" "}"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -262,7 +215,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"import java.util.concurrent.*\n", "import java.util.concurrent.*\n",
"\n", "\n",
@ -275,7 +227,9 @@
"}\n", "}\n",
"\n", "\n",
"future.get()" "future.get()"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -294,7 +248,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -303,18 +256,18 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"interface Observable{\n", "interface Observable{\n",
" fun onChange(callback: (Int) -> Unit)\n", " fun onChange(callback: (Int) -> Unit)\n",
"}\n", "}\n",
"\n", "\n",
"val observable: Observable by lazy{ TODO() }" "val observable: Observable by lazy{ TODO() }"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -323,14 +276,15 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"val list: MutableList<Int> = mutableListOf<Int>()\n", "val list: MutableList<Int> = mutableListOf<Int>()\n",
"\n", "\n",
"observable.onChange { \n", "observable.onChange { \n",
" list.add(it)\n", " list.add(it)\n",
"}" "}"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -349,7 +303,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -358,12 +311,12 @@
"type": "CODE" "type": "CODE"
} }
}, },
"source": [],
"outputs": [], "outputs": [],
"source": [] "execution_count": null
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -372,7 +325,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"val executor: ExecutorService = Executors.newFixedThreadPool(4)\n", "val executor: ExecutorService = Executors.newFixedThreadPool(4)\n",
"\n", "\n",
@ -381,7 +333,9 @@
" runLongTask(it)\n", " runLongTask(it)\n",
" }\n", " }\n",
"}" "}"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -400,7 +354,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -409,7 +362,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"import kotlin.io.path.Path\n", "import kotlin.io.path.Path\n",
"import kotlin.io.path.writeText\n", "import kotlin.io.path.writeText\n",
@ -423,7 +375,9 @@
" .forEach { \n", " .forEach { \n",
" file.writeText(it)\n", " file.writeText(it)\n",
" }" " }"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -442,7 +396,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -451,7 +404,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"val cache = mutableMapOf<Int, Int>\n", "val cache = mutableMapOf<Int, Int>\n",
"\n", "\n",
@ -466,7 +418,9 @@
" consumer(result)\n", " consumer(result)\n",
" }\n", " }\n",
"}" "}"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -500,7 +454,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -509,7 +462,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"import kotlin.concurrent.*\n", "import kotlin.concurrent.*\n",
"\n", "\n",
@ -517,11 +469,12 @@
" runLongTask()\n", " runLongTask()\n",
"}\n", "}\n",
"t.join()" "t.join()"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -530,8 +483,9 @@
"type": "CODE" "type": "CODE"
} }
}, },
"source": [],
"outputs": [], "outputs": [],
"source": [] "execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -550,7 +504,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -559,7 +512,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"val future = executor.submit<Int>{\n", "val future = executor.submit<Int>{\n",
" runLongTask()\n", " runLongTask()\n",
@ -567,7 +519,9 @@
"\n", "\n",
"future.get()\n", "future.get()\n",
"future.cancel(true)" "future.cancel(true)"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -586,7 +540,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -595,7 +548,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"import java.util.concurrent.*\n", "import java.util.concurrent.*\n",
"\n", "\n",
@ -613,7 +565,9 @@
"\n", "\n",
"cf2.join()\n", "cf2.join()\n",
"cf3.join()" "cf3.join()"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -632,7 +586,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -641,7 +594,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"import java.util.concurrent.locks.*\n", "import java.util.concurrent.locks.*\n",
"\n", "\n",
@ -666,7 +618,9 @@
"cf5.join()\n", "cf5.join()\n",
"\n", "\n",
"list" "list"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -677,9 +631,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"class A(val value: Int)\n", "class A(val value: Int)\n",
"class B(val value: Int)\n", "class B(val value: Int)\n",
@ -690,7 +642,9 @@
"val combined = aFlow.zip(bFlow){ a, b->\n", "val combined = aFlow.zip(bFlow){ a, b->\n",
" a.value + b.value\n", " a.value + b.value\n",
"}.debounce(2.seconds)" "}.debounce(2.seconds)"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -701,9 +655,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"sealed class Event {\n", "sealed class Event {\n",
" class Open(val transactionId: Int): Event()\n", " class Open(val transactionId: Int): Event()\n",
@ -732,7 +684,9 @@
" }\n", " }\n",
"\n", "\n",
"}" "}"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -766,7 +720,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -775,7 +728,6 @@
"type": "CODE" "type": "CODE"
} }
}, },
"outputs": [],
"source": [ "source": [
"import java.net.*\n", "import java.net.*\n",
"import java.net.http.*\n", "import java.net.http.*\n",
@ -807,7 +759,9 @@
" }\n", " }\n",
" .thenAccept{ println(it.toList()) }\n", " .thenAccept{ println(it.toList()) }\n",
" .join()" " .join()"
] ],
"outputs": [],
"execution_count": null
}, },
{ {
"attachments": {}, "attachments": {},
@ -826,7 +780,6 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": { "metadata": {
"datalore": { "datalore": {
"hide_input_from_viewers": true, "hide_input_from_viewers": true,
@ -835,8 +788,9 @@
"type": "CODE" "type": "CODE"
} }
}, },
"source": [],
"outputs": [], "outputs": [],
"source": [] "execution_count": null
} }
], ],
"metadata": { "metadata": {