From 6b3341eb073581c88d7cfb9b615e7d1c8b90d6c5 Mon Sep 17 00:00:00 2001 From: yostane <1958676+yostane@users.noreply.github.com> Date: Wed, 4 Dec 2024 00:47:37 +0100 Subject: [PATCH] api days notebook --- .../conferences/2024-12-04-apidays/.gitignore | 1 + .../2024-12-04-apidays/api-days2024.ipynb | 239 ++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 material/conferences/2024-12-04-apidays/.gitignore create mode 100644 material/conferences/2024-12-04-apidays/api-days2024.ipynb diff --git a/material/conferences/2024-12-04-apidays/.gitignore b/material/conferences/2024-12-04-apidays/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/material/conferences/2024-12-04-apidays/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/material/conferences/2024-12-04-apidays/api-days2024.ipynb b/material/conferences/2024-12-04-apidays/api-days2024.ipynb new file mode 100644 index 0000000..3677490 --- /dev/null +++ b/material/conferences/2024-12-04-apidays/api-days2024.ipynb @@ -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 {\n", + " if (dev == null){\n", + " return \"NONE\"\n", + " }\n", + " // Here dev is casted to Map\n", + " // ?: is the elvis operator\n", + " return (dev[\"name\"] ?: \"NONE\").uppercase()\n", + "}\n", + "fun getUppercaseNameSe(dev: Map?) = 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 +}