SaveSnippets
Community
Pricing
Sign In
Get Started
@admin
ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes
Showing
301–330
of
770
public snippets
Kotlin
reified Type Parameters
In an `inline` function, a `reified` type parameter survives erasure — you can use `T::class`, `is T`, `as T` at runtime. The killer use-case for JSON parsers ("give me a List<User>").
3h ago
0
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).
3h ago
0
Rust
Newtype Pattern (zero-cost wrappers)
Wrap a primitive in a tuple struct to give it a distinct type identity at the type level — without runtime cost. Replaces "is this number a UserId or a PostId?" bugs.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h ago
0
Bash
Cron Entry From Script
Append a cron job idempotently — grep first to avoid duplicates on re-run. Doesn't require editing files manually.
3h 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.
3h ago
0
Kotlin
Lambdas and Function Types
Kotlin functions are first-class values. A lambda is `{ args -> body }`; its type is `(InputTypes) -> ReturnType`. Single-argument lambdas can use the implicit `it` parameter.
3h 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.
3h 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).
3h 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).
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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.
3h 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).
3h 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.
3h ago
0
Go
database/sql — Open + Query
`database/sql` is driver-agnostic — pick a driver (`pgx/stdlib`, `go-sql-driver/mysql`, `mattn/sqlite3`). Always `defer db.Close()` only on app exit; the pool is meant to be long-lived.
3h ago
0
1
…
9
10
11
12
13
…
26