Skip to content

Commit

Permalink
add dsl and coroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
indramahkota committed Dec 19, 2021
1 parent 34cd87a commit f12c28c
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 1 deletion.
66 changes: 66 additions & 0 deletions .ipynb_checkpoints/Coroutines-checkpoint.ipynb
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
}
89 changes: 89 additions & 0 deletions .ipynb_checkpoints/Kotlin DSL-checkpoint.ipynb
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
}
77 changes: 77 additions & 0 deletions Coroutines.ipynb
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
}
89 changes: 89 additions & 0 deletions Kotlin DSL.ipynb
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
}
2 changes: 1 addition & 1 deletion belajar-krangl.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"name": "kotlin",
"nbconvert_exporter": "",
"pygments_lexer": "kotlin",
"version": "1.6.20-dev-5432"
"version": "1.6.20-dev-6372"
}
},
"nbformat": 4,
Expand Down

0 comments on commit f12c28c

Please sign in to comment.