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

Showing 121–150 of 770 public snippets

PHP
Minimal PSR-3 Logger
A 30-line PSR-3 compatible logger you can drop into any project that expects \Psr\Log\LoggerInterface. Writes to a file using the JSON structured format.
1m ago 0
Java
CountDownLatch — Wait for N Events
Block one or more threads until a count of events occurs. Set the count up front; each event calls `countDown()`; waiters call `await()`. Classic "wait until all workers are ready" pattern.
1m ago 0
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.
1m ago 0
PHP
URL Slug Generator (UTF-8 safe)
Convert any string into a clean URL-safe slug. Transliterates accented characters to ASCII, lowercases, replaces non-alphanumerics with dashes, and collapses runs of dashes. Works on UTF-8 input out of the box.
1m ago 0
PHP
Detect & Strip Byte Order Mark
Some text editors prepend a UTF-8 BOM (0xEF 0xBB 0xBF) which breaks header() calls, JSON, and PHP output. Detect and strip it defensively when ingesting external files.
1m ago 0
Kotlin
object Declaration — Singleton
`object` declares a thread-safe lazy singleton. Built into the language — no `private constructor + getInstance()` boilerplate. Also: anonymous `object : Interface { … }` for one-shot instances.
1m ago 0
PHP
CSV Writer with Proper Escaping
Write rows to a CSV file using fputcsv with sensible quoting. Also writes a UTF-8 BOM so Excel opens the file with the right encoding.
1m ago 0
Python
JWT Sign + Verify (PyJWT)
The de-facto JWT library for Python. HS256 demo with an exp claim and the standard "verify everything" decode flow. Mind that PyJWT raises specific exceptions you can catch separately.
1m ago 0
Rust
Custom Error Type with thiserror
The `thiserror` crate generates a clean `Error + Display + Debug` impl from an enum, with automatic `From` conversions. The library-author's error-type tool of choice.
1m ago 0
TypeScript
Retry with Exponential Backoff + Jitter
Retry an async operation up to N times, doubling the delay each attempt with random jitter to avoid thundering herd. Decide retryability via an optional predicate so 4xx errors stop immediately.
1m ago 0
Bash
PID File Management
Write a daemon's PID to a file at start, check on subsequent runs, clean up on exit. Avoids two copies of the same daemon when the script is invoked twice.
1m ago 0
SQL
ROLLUP — Subtotals and Grand Totals
`GROUP BY ROLLUP` adds subtotal rows (with NULL for the rolled-up columns) plus a grand total. Drop into reports without writing UNIONs by hand.
1m ago 0
Java
try-with-resources
For any `AutoCloseable` (files, streams, DB connections, locks), declare it in `try(...)` and Java auto-calls `close()` even on exception. Replaces error-prone `try / finally` blocks.
1m ago 0
PHP
Average / Median / Mode
Three classic descriptive statistics over a numeric array. Built without any external math library — just sort + count.
1m ago 0
Kotlin
run — Compute With Scope
`x.run { ... }` is `apply` but returns the block's LAST expression instead of `x`. Top-level `run { ... }` (no receiver) is useful for inline computation blocks.
1m ago 0
TypeScript
uniqueBy — Dedupe by Callback
Deduplicate an array using a derived key (object id, lowercased email, etc.). First occurrence wins. Backed by a Map for O(n) performance.
1m ago 0
HTML
Native <dialog> Modal
The HTML `<dialog>` element gives you a real modal — focus trap, Escape-to-close, backdrop overlay — without any JS library. Method `.showModal()` opens; `.close()` closes.
1m ago 0
TypeScript
Debounce (typed)
Wait until the caller stops invoking for `delay` ms, then call the function with the most recent arguments. The TS version preserves the original signature so the returned function has the same parameters.
1m ago 0
Bash
Sort by a Specific Column
Sort takes -k for column-based ordering and -t to set the delimiter. -h sorts human-readable sizes ("1.5G", "300K") correctly.
1m ago 0
Rust
flat_map and Iterator::flatten
`flat_map(f)` is `map(f).flatten()` — map each item to an iterator, then concatenate. Indispensable for working with nested structures.
1m ago 0
Kotlin
MockK — Kotlin-Idiomatic Mocking
MockK plays well with Kotlin: final classes, coroutines, top-level functions, all mockable. `every { ... } returns ...` is the canonical setup.
1m ago 0
Kotlin
Type Aliases
`typealias` gives a shorter / more meaningful name to an existing type. Compile-time only — no new type, no overhead. Great for taming verbose function types or generic parameters.
1m 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.
1m 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.
1m ago 0
Python
Deprecation Warning Decorator
Mark a function as deprecated so callers get a DeprecationWarning the first time it's called. Includes the replacement function name in the message so callers know what to switch to.
1m ago 0
Kotlin
when Expressions — Powerful switch
Kotlin's `when` is an expression (returns a value), supports ranges, type checks, multiple values per branch, and arbitrary boolean conditions. Replaces nested if/else AND traditional switch.
1m ago 0
SQL
LATERAL Join — Per-Row Subquery
PostgreSQL `LATERAL` lets a join's right-hand side reference the left-hand row — like a correlated subquery, but supplying multiple columns or rows. Perfect for "top-N per group".
1m ago 0
Bash
Unique While Preserving Order
`sort -u` re-orders. `awk '!seen[$0]++'` is the classic order-preserving dedupe — and it's a single line.
1m 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.
1m ago 0
Java
flatMap — Flatten Nested Streams
Map each element to a Stream, then concatenate all of them into one. Used everywhere from "all words from a list of sentences" to "all permissions from a list of roles".
1m ago 0