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

Showing 271–300 of 770 public snippets

JavaScript
Range Generator
Generates an array of numbers from start to end (inclusive) with an optional step. Supports negative steps for counting down. Covers the most common use case of Array.from({ length: n }, (_, i) => i) with a much cleaner API, and handles arbitrary start/end/step combinations.
55m ago 0
Java
JUnit 5 — Parameterized Tests
`@ParameterizedTest` runs the same test with multiple inputs. Use `@CsvSource`, `@MethodSource`, or `@EnumSource` for data. Eliminates copy-paste in test classes.
55m ago 0
PHP
Stream Large CSV with Generator
Iterate over a CSV file row-by-row without ever loading the whole file into memory. Uses a generator so consumers can foreach naturally and PHP cleans up the file handle.
55m ago 0
Go
struct Tags for JSON Marshaling
Backtick string tags on struct fields control how `encoding/json` serializes them — rename to camelCase, omit zero values, skip entirely. The same tag system is used by many other libraries.
55m ago 0
HTML
JSON-LD Structured Data (Schema.org)
JSON-LD is Google's preferred way to mark up structured data for rich search results. Drop it in a <script type="application/ld+json"> tag in <head>. Validate with Google's Rich Results Test.
55m ago 0
Bash
Read Single Keystroke Without Enter
For menus and confirm prompts where you want one-key response (no need to press Enter). -n 1 reads exactly one character, -s silences the echo.
55m ago 0
Java
BufferedReader / BufferedWriter
For large files or line-oriented protocols, wrap a Reader/Writer in `Buffered*`. Reduces per-call overhead from many small reads to fewer large ones. Always close with try-with-resources.
55m ago 0
SQL
ROW_NUMBER OVER PARTITION
Number rows within a partition — the most common "give me the Nth thing per group" pattern. Pair with a WHERE rn = 1 in an outer query to keep just the top row per group.
55m ago 0
Go
Benchmarks with testing.B
`BenchmarkXxx(b *testing.B)` functions are run by `go test -bench`. `b.N` is the loop count the framework auto-tunes; report results in ns/op, allocs/op.
55m ago 0
Kotlin
with — Scope an Existing Object
`with(x) { ... }` is `run` flipped: the receiver is the first argument. Reads naturally when you're doing several things to an existing object without chaining.
55m ago 0
HTML
Video Element with Captions
`<video>` with multiple `<source>` formats (MP4 + WebM), `controls`, `poster`, and a captions track via `<track kind="captions">`. All shipped with the browser — no JS player needed.
55m ago 0
Java
ExecutorService — Thread Pools
`ExecutorService` is the modern way to manage threads. Submit Callables/Runnables, get Futures back. Always shutdown the executor — and prefer try-with-resources on Java 19+ where ExecutorService is AutoCloseable.
55m ago 0
SQL
Conditional Aggregation (FILTER / CASE)
Count or sum subsets of rows in a single GROUP BY pass. PostgreSQL has the cleaner `FILTER` clause; everyone else uses `SUM(CASE WHEN ...)`.
55m ago 0
TypeScript
Bearer-Auth Fetch Wrapper
Wrap fetch so every request automatically attaches an Authorization: Bearer header from a token getter. Token can be a static string or a function (useful for refreshing).
55m ago 0
Kotlin
apply — Configure Object, Return Itself
`x.apply { ... }` runs the block with `x` as `this` (so you call methods/set properties directly) and returns `x`. The canonical builder-style configuration.
55m ago 0
Python
Retry Decorator with Exponential Backoff
Generic retry decorator: attempts × base delay, exponential ramp, random jitter. Filter retryable exceptions via the `on` tuple so logical errors aren't retried.
55m 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.
55m ago 0
Rust
Untyped JSON with serde_json::Value
When you don't know the JSON shape up-front, parse into `serde_json::Value` and navigate via index / get(). Type-safe pattern matching converts back to Rust types.
55m ago 0
Rust
Read a File — Whole / Lines / Bytes
Three common patterns: load it all into memory, iterate line-by-line, or stream bytes. Pick by file size — `read_to_string` is convenient but bad for huge files.
55m ago 0
Rust
match with Guards, Ranges, and Bindings
`match` arms can have `if` guards, range patterns (`1..=5`), bindings (`x @ pattern`), and ORs (`A | B`). Combine them for very expressive dispatch.
55m ago 0
PHP
HTTP Retry with Exponential Backoff
Wrap any HTTP call in a retry loop with capped exponential backoff and jitter. Retries on 5xx / network errors but not on 4xx (those won't fix themselves).
55m ago 0
SQL
RETURNING — Get Back What You Wrote
PostgreSQL `RETURNING *` returns the rows affected by an INSERT/UPDATE/DELETE — no separate SELECT round-trip needed. SQL Server has `OUTPUT`; MySQL added `RETURNING` in MariaDB and 8.0.31+.
55m 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.
55m ago 0
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.
55m ago 0
Kotlin
kotlin.test — Basic Tests
`kotlin.test` is the multiplatform test stdlib. `@Test` marks a test; `assertEquals`, `assertTrue`, `assertFailsWith` are the workhorses.
55m ago 0
Python
pairwise — Rolling Pair Iterator
`pairwise([a, b, c, d])` yields `(a, b), (b, c), (c, d)` — built into itertools since 3.10. Perfect for "diff consecutive elements" patterns (deltas, rate-of-change, validation between rows).
55m 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.
55m ago 0
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".
55m ago 0
JavaScript
Random Hex Color
Generates a random 6-digit hex colour string. Useful for seeding avatar backgrounds, chart series colours, placeholder UI elements, and testing colour-dependent components. The crypto version produces a more unpredictable result suitable for generating unique palette tokens.
55m ago 0
Bash
Read File Line-By-Line (the safe way)
The classic `for line in $(cat file)` is wrong — it word-splits and globbing fires on filenames. Use `while IFS= read -r line` with input redirection.
55m ago 0