#kotlin Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Kotlin init Block + Constructor Order
`init { }` blocks run during construction, in declaration order, interleaved with property initializers. Use to validate the primary constructor or set up derived state.
Kotlin repeat, also lateinit and isInitialized
`repeat(n) { i -> ... }` for N-times loops where you don't care about index naming. `lateinit var` lets you declare a non-null var without initializing it — common for DI / framework injection.
Kotlin windowed and chunked
`chunked(n)` splits a list into NON-overlapping pieces of size n. `windowed(n)` slides a fixed-size window across — overlapping. Useful for moving averages, n-grams, batch APIs.
Kotlin associate and associateBy
`associateBy` builds a `Map<K, T>` keyed by a derived value. `associateWith` keys by the items themselves with derived values. `associate` lets you build both key and value from each item.
Kotlin minOf / maxOf / sumOf / averageOf
Numerical reducers with a selector lambda — much cleaner than `.map { ... }.max()` chains. `sumOf` is type-aware (returns Int, Long, Double, BigDecimal as appropriate).
Kotlin OkHttp Client (Java interop)
OkHttp is the JVM-standard HTTP library. Works fine from Kotlin; use the `await()` extension from `okhttp3.coroutines` to get suspend support.
Kotlin java.time — When You Need JVM Interop
On the JVM, `java.time` is fine — and often necessary for interop with Java APIs. Kotlin's extension methods make it ergonomic enough that you might never reach for kotlinx-datetime.
Kotlin Sequences — Lazy Collections
A `List` materializes every intermediate `map`/`filter` result. A `Sequence` processes ONE element through the whole pipeline at a time — better for big inputs or short-circuiting (`first`, `take`).
Kotlin Compose State Hoisting
Lift state UP to where it's needed. A composable becomes "stateless" (taking `value` + `onValueChange`) which makes it reusable and testable. The Compose mantra.
Kotlin JDBC with use { } — Auto-Close Connections
Plain JDBC is fine for simple cases. Wrap connections / statements / result sets in `use { }` for guaranteed cleanup even on exceptions.
Kotlin Data Classes — Records Done Right
`data class` auto-generates `equals`, `hashCode`, `toString`, `copy`, and `componentN` (destructuring). Replaces boilerplate POJOs / Records / classes-with-lombok in one line.
Kotlin Ktor Server — Hello World
Ktor lets you stand up an HTTP server in a few lines. Pick an engine (Netty / Jetty / CIO), declare routes, run. Coroutine-native end to end.
Kotlin Ktor Server — JSON API with kotlinx.serialization
Install `ContentNegotiation` + `Json` plugin and you get typed (de)serialization for every route. Decode request bodies into data classes; respond with data classes.
Kotlin Ktor Server — Middleware (Plugins)
Plugins wrap every request — logging, CORS, auth, rate limiting. Install with `install(Plugin) { config }` at the application level.
Kotlin kotlinx.serialization — JSON Basics
Compile-time code generation for JSON serialization. Annotate `@Serializable` on a data class; `Json.encodeToString` and `Json.decodeFromString` are typed.
Kotlin Smart Casts
After a successful `is` check (or null check), the compiler "smart-casts" the variable to the narrower type in that scope. No explicit cast needed.
Kotlin kotlinx-datetime — Multiplatform Dates
`kotlinx-datetime` is the multiplatform date/time library — works on JVM, JS, Native. `Instant` (UTC moment), `LocalDate`, `TimeZone` are the building blocks.
Kotlin Write to a File
`writeText` overwrites; `appendText` appends. For multiple writes, use `bufferedWriter().use { }` for efficiency + auto-close.