#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 groupBy and partition
`groupBy` returns a `Map<Key, List<T>>` keyed by what the lambda returns. `partition` is a special-case `groupBy` for booleans — returns `(matching, nonMatching)` Pair.
Kotlin fold and scan
`fold` accumulates from a seed across all items. `runningFold` / `scan` yields every intermediate accumulator value — running totals, cumulative metrics.
Kotlin Infix Functions
A single-arg method/extension can be called without dot or parens when marked `infix`. Lets you write DSL-style code like `5 shouldBe 5` or `key to value`.
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 map / filter / reduce
The three classic functional combinators. Each takes a lambda and returns a new collection (or a single value for reduce). Chain freely — intermediate allocations only matter at very large scale (use `Sequence` then).
Kotlin StateFlow — Observable State
`StateFlow<T>` is a hot Flow holding a single current value. Perfect for UI state in Compose/Android — always has a value, conflates intermediate values, multi-subscriber.
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 Enums with Properties and Methods
Kotlin enums can carry constructor properties and define methods — including abstract methods overridden per constant. Way more expressive than Java enums.
Kotlin ViewModel + StateFlow + Compose
Standard Android pattern: ViewModel holds state in a `StateFlow`, Compose collects it as state. Survives config changes; isolates business logic from UI.
Kotlin kotlin.time — Duration and measureTime
`kotlin.time.Duration` is a typesafe time amount with friendly literal syntax (`5.seconds`, `2.minutes`). `measureTime { }` benchmarks a block.
Kotlin Result Type — Safe Error-Returning APIs
`Result<T>` wraps "success or thrown exception" without forcing the caller into try/catch. `runCatching { ... }` is the standard producer.
Kotlin Custom Exception + Sealed Result Pattern
For library / service code, define your own exception hierarchy. Pair with `sealed` + `when` for type-safe error handling at the call site.
Kotlin Sealed Interfaces (Kotlin 1.5+)
Like sealed classes, but for interfaces — and a class can implement multiple sealed interfaces. Cleaner state modeling when you want "this is both a Loadable and a Cacheable".
Kotlin Generic Class and Function
`<T>` declares a type parameter. Use it on classes (`Box<T>`) and functions (`fun <T> identity(x: T) = x`). Type bounds (`T : Comparable<T>`) constrain what T can be.
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 Room — Android Database
Room is the official Android persistence library — annotation-driven, compile-time SQL verification, coroutine + Flow integration out of the box.
Kotlin Jetpack Compose — Basic Composable
Compose is Android's modern declarative UI toolkit. A `@Composable` function describes UI; the runtime re-runs it when inputs change. Replaces XML layouts entirely.
Kotlin Exposed — Kotlin-First ORM
JetBrains' Exposed combines a SQL DSL with a lightweight ORM. Either style is fully type-safe; both compile to plain SQL.