-
Notifications
You must be signed in to change notification settings - Fork 5
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
Showing
2 changed files
with
238 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
out/ |
236 changes: 236 additions & 0 deletions
236
material/conferences/2024-11-12-paris-jug/idiomatic-01.ipynb
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,236 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"metadata": {}, | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Idiomatic Kotlin @ Paris JUG\n", | ||
"Date: 2024 / 10 / 12" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"collapsed": true, | ||
"ExecuteTime": { | ||
"end_time": "2024-10-25T14:33:51.646533Z", | ||
"start_time": "2024-10-25T14:33:51.606114Z" | ||
} | ||
}, | ||
"source": [ | ||
"val event = \"Paris JUG\"\n", | ||
"println(\"Hello $event\") // String template" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Hello Paris JUG\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 17 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-25T14:33:51.733128900Z", | ||
"start_time": "2024-10-25T14:33:51.666925500Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// Extension function: add a function to a class without inhetitance\n", | ||
"fun String.randomCase(): String {\n", | ||
" // If expression\n", | ||
" return if (Math.random() > 0.5) uppercase() else lowercase()\n", | ||
"}\n", | ||
"println(event.randomCase())" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"PARIS JUG\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 18 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-25T14:33:51.861854200Z", | ||
"start_time": "2024-10-25T14:33:51.755062200Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// Default values for arguments\n", | ||
"fun String.randomCaseChance(chance: Double = 0.5, offset: Int = 0) =\n", | ||
" // Single-expression functions with = (return type is inferred)\n", | ||
" (if (Math.random() > chance) uppercase() else lowercase()).drop(offset)\n", | ||
"// Argument labels\n", | ||
"println(event.randomCaseChance(offset = 3))\n", | ||
"println(event.randomCaseChance(\n", | ||
" chance = 0.9,\n", | ||
" offset = 2,\n", | ||
" )\n", | ||
")" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"is jug\r\n", | ||
"ris jug\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 19 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-25T14:33:52.069576Z", | ||
"start_time": "2024-10-25T14:33:51.987387600Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// Functions are 1st class citizen\n", | ||
"fun String.transformToInt(transformer: (String) -> Int) = transformer(this)\n", | ||
"// Parenthesis can be omitted for last lambda\n", | ||
"// \"it\" is implicit for single-argument lambdas\n", | ||
"// return is optionnal (in majot cases)\n", | ||
"val count = event.transformToInt { it.length }\n", | ||
"println(count)" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"9\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 20 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-25T14:33:52.233616600Z", | ||
"start_time": "2024-10-25T14:33:52.158182800Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"typealias ST = String.() -> String\n", | ||
"fun String.transformToString(transformer: ST) = transformer(this)\n", | ||
"// String.() -> String means that the lambda has access to the string as this\n", | ||
"// Allows to define DSL\n", | ||
"val uppercased = event.transformToString { uppercase() }\n", | ||
"print(\"Uppercased: $event\")" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Uppercased: Paris JUG" | ||
] | ||
} | ||
], | ||
"execution_count": 21 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-25T14:40:24.001881Z", | ||
"start_time": "2024-10-25T14:40:23.881908900Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// Filter, map, reduce out of the box (no need to convert to stream)\n", | ||
"println(\n", | ||
" event.map { if (Math.random() > 0.5) it.uppercaseChar() else it.lowercaseChar() }.joinToString()\n", | ||
")\n", | ||
"// More operators available\n", | ||
"println(\n", | ||
" event.zip(\"2024\") { left, right -> \"$left/$right\" }\n", | ||
")" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"P, a, r, I, S, , J, u, g\r\n", | ||
"[P/2, a/0, r/2, i/4]\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 24 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-10-25T14:50:19.815828200Z", | ||
"start_time": "2024-10-25T14:50:19.702384500Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// ? Means that the argument can be null\n", | ||
"fun getUppercaseName(dev: Map<String, String>?): String {\n", | ||
" if (dev == null){\n", | ||
" return \"NONE\"\n", | ||
" }\n", | ||
" // Here dev is casted to Map<String, String>\n", | ||
" // ?: is the elvis operator\n", | ||
" return (dev[\"name\"] ?: \"NONE\").uppercase()\n", | ||
"}\n", | ||
"fun getUppercaseNameSe(dev: Map<String, String>?) = dev?.get(\"name\")?.uppercase() ?: \"NONE\"\n", | ||
"val dev = mapOf(\"name\" to \"Yassine\", \"Meetup\" to \"ParisJug\")\n", | ||
"println(dev)\n", | ||
"println(getUppercaseName(dev))\n", | ||
"println(getUppercaseNameSe(dev))" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{name=Yassine, Meetup=ParisJug}\r\n", | ||
"YASSINE\r\n", | ||
"YASSINE\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 27 | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Kotlin", | ||
"language": "kotlin", | ||
"name": "kotlin" | ||
}, | ||
"language_info": { | ||
"name": "kotlin", | ||
"version": "1.9.23", | ||
"mimetype": "text/x-kotlin", | ||
"file_extension": ".kt", | ||
"pygments_lexer": "kotlin", | ||
"codemirror_mode": "text/x-kotlin", | ||
"nbconvert_exporter": "" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |