-
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
240 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 @@ | ||
.idea/ |
239 changes: 239 additions & 0 deletions
239
material/conferences/2024-12-04-apidays/api-days2024.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,239 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"metadata": {}, | ||
"cell_type": "markdown", | ||
"source": "# Idiomatic Kotlin @ API Days Paris 2024" | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-03T23:23:52.926394100Z", | ||
"start_time": "2024-12-03T23:23:52.833072600Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"data class Event(val name: String, val year:Int) // One line class\n", | ||
"val event = Event(\"API Days\", 2024)\n", | ||
"println(event) // To string implemented out of the box\n", | ||
"println(\"Hello ${event.name}\") // String template" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Event(name=API Days, year=2024)\r\n", | ||
"Hello API Days\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 12 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-03T23:23:53.033397Z", | ||
"start_time": "2024-12-03T23:23:52.947644900Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// Extension function: add a method 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.name.randomCase())" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"API DAYS\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 13 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-03T23:23:53.160407300Z", | ||
"start_time": "2024-12-03T23:23:53.047728100Z" | ||
} | ||
}, | ||
"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.name.randomCaseChance(offset = 3))\n", | ||
"println(event.name.randomCaseChance(\n", | ||
" offset = 2,\n", | ||
" chance = 0.9,\n", | ||
" )\n", | ||
")" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
" DAYS\r\n", | ||
"i days\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 14 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-03T23:23:53.264804800Z", | ||
"start_time": "2024-12-03T23:23:53.171326300Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// Functions are 1st class citizen\n", | ||
"fun String.transformToInt(transformer: (String) -> Int) = transformer(this)\n", | ||
"// return is optionnal (in many situations)\n", | ||
"val count0 = event.name.transformToInt({ s -> s.length })\n", | ||
"// Parenthesis can be omitted for last lambda\n", | ||
"// \"it\" is implicit for single-argument lambdas\n", | ||
"val count = event.name.transformToInt { it.length }\n", | ||
"println(count)" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"8\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 15 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-03T23:23:53.588578700Z", | ||
"start_time": "2024-12-03T23:23:53.424789800Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"fun String.transformToString(transformer: String.() -> String) = transformer(this)\n", | ||
"// String.() -> String means that the lambda has access to the string as \"this\"\n", | ||
"val uppercased0 = event.name.transformToString { this.uppercase() }\n", | ||
"// We usually omit this. This is the building block of DSLs (Domain Specific languages)\n", | ||
"val uppercased = event.name.transformToString { uppercase() }\n", | ||
"print(\"Uppercased: $uppercased\")" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Uppercased: API DAYS" | ||
] | ||
} | ||
], | ||
"execution_count": 16 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-03T23:23:53.834073100Z", | ||
"start_time": "2024-12-03T23:23:53.598859900Z" | ||
} | ||
}, | ||
"cell_type": "code", | ||
"source": [ | ||
"// Filter, map, reduce out of the box\n", | ||
"println(\n", | ||
" event.name.map {\n", | ||
" if (Math.random() > 0.5) it.uppercaseChar() else it.lowercaseChar()\n", | ||
" }.joinToString()\n", | ||
")\n", | ||
"// More operators available\n", | ||
"println(\n", | ||
" event.name.zip(\"2024\") { left, right -> \"$left/$right\" }\n", | ||
")" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"A, p, i, , d, a, Y, s\r\n", | ||
"[A/2, P/0, I/2, /4]\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 17 | ||
}, | ||
{ | ||
"metadata": { | ||
"ExecuteTime": { | ||
"end_time": "2024-12-03T23:23:54.122576Z", | ||
"start_time": "2024-12-03T23:23:53.854138200Z" | ||
} | ||
}, | ||
"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", | ||
"// Kotlin guarantees NPE free code at compile time (unless you use !!)\n", | ||
"val dev = mapOf(\"name\" to \"Yassine\", \"Event\" to event.name)\n", | ||
"println(dev)\n", | ||
"println(getUppercaseName(dev))\n", | ||
"println(getUppercaseNameSe(dev))" | ||
], | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{name=Yassine, Event=API Days}\r\n", | ||
"YASSINE\r\n", | ||
"YASSINE\r\n" | ||
] | ||
} | ||
], | ||
"execution_count": 18 | ||
} | ||
], | ||
"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 | ||
} |