admin
@admin ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes

Showing 361–390 of 770 public snippets

Kotlin
Variance — in vs out
`out T` (covariant) → producer of T; can be assigned to a wider type. `in T` (contravariant) → consumer of T; can be assigned to a narrower type. Without modifiers, generics are invariant (strict match).
4h ago 0
Kotlin
JSON Annotations — Rename, Default, Optional
Per-field annotations let you map Kotlin camelCase ↔ JSON snake_case, set defaults for missing fields, and skip nulls.
4h ago 0
Kotlin
Type-Safe Builder DSL
`@DslMarker` + lambdas with receivers let you build statically-typed DSLs that look like declarative configuration. The HTML / Gradle Kotlin DSL style.
4h ago 0
Kotlin
tailrec — Tail-Call Optimization
Mark a recursive function `tailrec` and the compiler rewrites it as a loop — no stack frame per call, no StackOverflowError on deep recursion. Function must end with itself as the FINAL expression.
4h ago 0
PHP
Dump Variable to Browser (var_dump replacement)
A nicer var_dump-style dumper that wraps output in <pre> with monospaced font for browser inspection. Drop-in replacement for var_dump while debugging.
4h ago 0
TypeScript
Type-Safe Event Emitter
A 30-line type-safe mini EventEmitter. The event-name → payload map is enforced at compile time, so emit and on calls can't drift apart.
4h ago 0
Python
Walk Directory Tree (Filtered)
Recursively yield every file under a directory matching a predicate. Built on pathlib.rglob; the predicate gives you precise control without listing the whole tree up front.
4h ago 0
Bash
Clean Up Merged Branches
Delete every local branch that's been merged to main. Filters out main/master and the current branch as a safety net.
4h ago 0
TypeScript
Conditional Types Basics
`T extends U ? X : Y` lets types branch on shape. Combine with `infer` to extract pieces of complex types — the building block under `ReturnType`, `Parameters`, and most utility libraries.
4h ago 0
Bash
Sort an Array
Bash itself doesn't sort arrays — you pipe through `sort`. readarray captures the sorted output back into an array, preserving each element verbatim (including spaces).
4h ago 0
Bash
Check Command Exists Before Using It
`command -v` is the portable, fast way to check whether a binary is on $PATH. Avoid `which` (its behavior varies between systems).
4h ago 0
Bash
Disk Space Alert
Cron-friendly script that checks disk usage on each filesystem and emails (or webhooks) if any partition exceeds a threshold.
4h ago 0
Bash
Rotate Logs Manually
If a service doesn't hook into logrotate, you can still rotate a log file yourself in one safe pattern: rename current → .1, truncate the original, signal the process to re-open if needed.
4h ago 0
Bash
docker exec — Drop Into Running Container
`docker exec` is your debugger. Get a shell, inspect env, tail logs, run one-off commands inside a live container.
4h ago 0
Go
errors.Join — Aggregate Multiple Errors
Go 1.20+ ships `errors.Join`, which combines multiple errors into one. Stop accumulating errors in a slice and stringifying yourself — use the standard library.
4h ago 0
Go
sync/atomic — Lock-Free Counter
For simple counters and flags, atomic ops are much faster than `sync.Mutex`. Go 1.19+ added typed wrappers (`atomic.Int64`) — clearer than the raw functions.
4h ago 0
SQL
CREATE TABLE with Constraints
Declare data integrity rules right in the schema — primary keys, foreign keys, unique constraints, NOT NULL, CHECK constraints, defaults. The DB enforces them so application bugs can't corrupt your data.
4h ago 0
Kotlin
Path Operations with java.nio.file
Modern Kotlin code uses `java.nio.file.Path` over the legacy `File`. Operator overloads + Kotlin extensions make it ergonomic.
4h ago 0
Kotlin
Parameterized Tests with JUnit 5
JUnit 5 + Kotlin: `@ParameterizedTest` runs the same test body with multiple input rows. `@CsvSource` for inline data, `@MethodSource` for complex args.
4h ago 0
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.
4h ago 0
PHP
Recursive Directory Walker
Yield every file under a directory using a generator and SPL's RecursiveIteratorIterator. Filter by extension or any other predicate without slurping all paths into memory first.
4h ago 0
Bash
Download with Progress Bar
Show a progress bar for long downloads. wget gives a great built-in bar; curl needs an explicit flag.
4h ago 0
Bash
Tar Archive with Timestamp
Pack up a directory with a date-stamped filename for easy backups. -z for gzip, -j for bzip2, -J for xz (smallest, slowest).
4h ago 0
Go
Variadic Functions
`...T` in the last parameter slot accepts zero or more T values. Pass a slice by suffixing with `...` to spread it. Used everywhere from `fmt.Println` to custom builders.
4h ago 0
Go
Signal Handling with signal.NotifyContext
Go 1.16+ `signal.NotifyContext` returns a context that's canceled on the listed signals. Cleaner than the legacy `signal.Notify(channel)` dance for graceful shutdown.
4h ago 0
PHP
Generate Identicon Avatar
Render a deterministic 5x5 symmetric "identicon" placeholder avatar from any seed (usually a user's email or username). No external service — pure GD.
4h ago 0
Bash
Sum a Column with awk
Awk's default field splitter is whitespace; pass a single character with -F. Perfect for summing the Nth column of a log file or CSV.
4h ago 0
Bash
Get Public IP Address
Several free services return your public IP as plain text. Useful when scripts running behind NAT need to know what address the outside world sees.
4h ago 0
Bash
Check Systemd Service Is Running
`systemctl is-active` is the right tool — exit code 0 if running, non-zero otherwise. Combine with restart-on-fail for poor-man's supervision.
4h ago 0
Bash
Create User With Sensible Defaults
Provision a new user account with a home directory, default shell, and sudo membership. Idempotent — re-running on an existing user is a no-op.
4h ago 0