-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
34cd87a
commit f12c28c
Showing
5 changed files
with
322 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "a4055fdf", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%use coroutines" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "632b11a3", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import java.time.Instant\n", | ||
"\n", | ||
"inline fun time(action: () -> Unit): Long {\n", | ||
" val start = Instant.now().toEpochMilli()\n", | ||
" action()\n", | ||
" return Instant.now().toEpochMilli() - start\n", | ||
"}\n", | ||
"\n", | ||
"suspend fun doWorld() = coroutineScope { // this: CoroutineScope\n", | ||
" launch {\n", | ||
" delay(1000L)\n", | ||
" println(\"World!\")\n", | ||
" }\n", | ||
" println(\"Hello\")\n", | ||
"}\n", | ||
"\n", | ||
"fun main() = runBlocking {\n", | ||
" time {\n", | ||
" doWorld()\n", | ||
" println(\"Lots of code\")\n", | ||
" }\n", | ||
" println(\"Done\")\n", | ||
"}\n", | ||
"\n", | ||
"main()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Kotlin", | ||
"language": "kotlin", | ||
"name": "kotlin" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "text/x-kotlin", | ||
"file_extension": ".kt", | ||
"mimetype": "text/x-kotlin", | ||
"name": "kotlin", | ||
"nbconvert_exporter": "", | ||
"pygments_lexer": "kotlin", | ||
"version": "1.6.20-dev-6372" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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,89 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 17, | ||
"id": "bd970b0f", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"root\n", | ||
" -> math\n", | ||
" -> algebra\n", | ||
" -> linear program\n", | ||
" -> trigonometry\n", | ||
" -> science\n", | ||
" -> physics" | ||
] | ||
}, | ||
"execution_count": 17, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"class TreeNode(val name: String) {\n", | ||
" val children = mutableListOf<TreeNode>()\n", | ||
"\n", | ||
" fun node(name: String, initialize: (TreeNode.() -> Unit)? = null) {\n", | ||
" val child = TreeNode(name)\n", | ||
" children.add(child)\n", | ||
" if (initialize != null) {\n", | ||
" child.initialize()\n", | ||
" }\n", | ||
" }\n", | ||
" \n", | ||
" fun printTree(): String {\n", | ||
" var output = name\n", | ||
" for (str in children) {\n", | ||
" output += \"\\n -> ${str.printTree()}\"\n", | ||
" }\n", | ||
" return output\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"fun tree(name: String, initialize: (TreeNode.() -> Unit)? = null): TreeNode {\n", | ||
" val root = TreeNode(name)\n", | ||
" if (initialize != null) {\n", | ||
" root.initialize()\n", | ||
" }\n", | ||
" return root\n", | ||
"}\n", | ||
"\n", | ||
"val t = tree(\"root\") {\n", | ||
" node(\"math\") {\n", | ||
" node(\"algebra\") {\n", | ||
" node(\"linear program\")\n", | ||
" }\n", | ||
" node(\"trigonometry\")\n", | ||
" }\n", | ||
" node(\"science\") {\n", | ||
" node(\"physics\")\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"t.printTree()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Kotlin", | ||
"language": "kotlin", | ||
"name": "kotlin" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "text/x-kotlin", | ||
"file_extension": ".kt", | ||
"mimetype": "text/x-kotlin", | ||
"name": "kotlin", | ||
"nbconvert_exporter": "", | ||
"pygments_lexer": "kotlin", | ||
"version": "1.6.20-dev-6372" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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,77 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "d3b17776", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%use coroutines" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "4b45afbc", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Hello\n", | ||
"World!\n", | ||
"Lots of code\n", | ||
"Done\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import java.time.Instant\n", | ||
"\n", | ||
"inline fun time(action: () -> Unit): Long {\n", | ||
" val start = Instant.now().toEpochMilli()\n", | ||
" action()\n", | ||
" return Instant.now().toEpochMilli() - start\n", | ||
"}\n", | ||
"\n", | ||
"suspend fun doWorld() = coroutineScope { // this: CoroutineScope\n", | ||
" launch {\n", | ||
" delay(1000L)\n", | ||
" println(\"World!\")\n", | ||
" }\n", | ||
" println(\"Hello\")\n", | ||
"}\n", | ||
"\n", | ||
"fun main() = runBlocking {\n", | ||
" time {\n", | ||
" doWorld()\n", | ||
" println(\"Lots of code\")\n", | ||
" }\n", | ||
" println(\"Done\")\n", | ||
"}\n", | ||
"\n", | ||
"main()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Kotlin", | ||
"language": "kotlin", | ||
"name": "kotlin" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "text/x-kotlin", | ||
"file_extension": ".kt", | ||
"mimetype": "text/x-kotlin", | ||
"name": "kotlin", | ||
"nbconvert_exporter": "", | ||
"pygments_lexer": "kotlin", | ||
"version": "1.6.20-dev-6372" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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,89 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 27, | ||
"id": "f102a82c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"root\n", | ||
" -> math\n", | ||
" -> algebra\n", | ||
" -> linear program\n", | ||
" -> trigonometry\n", | ||
" -> science\n", | ||
" -> physics" | ||
] | ||
}, | ||
"execution_count": 27, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"class TreeNode(val name: String) {\n", | ||
" val children = mutableListOf<TreeNode>()\n", | ||
"\n", | ||
" fun node(name: String, initialize: (TreeNode.() -> Unit)? = null) {\n", | ||
" val child = TreeNode(name)\n", | ||
" children.add(child)\n", | ||
" if (initialize != null) {\n", | ||
" child.initialize()\n", | ||
" }\n", | ||
" }\n", | ||
" \n", | ||
" fun printTree(): String {\n", | ||
" var output = name\n", | ||
" for (str in children) {\n", | ||
" output += \"\\n -> ${str.printTree()}\"\n", | ||
" }\n", | ||
" return output\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"fun tree(name: String, initialize: (TreeNode.() -> Unit)? = null): TreeNode {\n", | ||
" val root = TreeNode(name)\n", | ||
" if (initialize != null) {\n", | ||
" root.initialize()\n", | ||
" }\n", | ||
" return root\n", | ||
"}\n", | ||
"\n", | ||
"val t = tree(\"root\") {\n", | ||
" node(\"math\") {\n", | ||
" node(\"algebra\") {\n", | ||
" node(\"linear program\")\n", | ||
" }\n", | ||
" node(\"trigonometry\")\n", | ||
" }\n", | ||
" node(\"science\") {\n", | ||
" node(\"physics\")\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"t.printTree()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Kotlin", | ||
"language": "kotlin", | ||
"name": "kotlin" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": "text/x-kotlin", | ||
"file_extension": ".kt", | ||
"mimetype": "text/x-kotlin", | ||
"name": "kotlin", | ||
"nbconvert_exporter": "", | ||
"pygments_lexer": "kotlin", | ||
"version": "1.6.20-dev-6372" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
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