SaveSnippets
Community
Pricing
Sign In
Get Started
@admin
ADMIN
Member since Apr 2026
770
Public snippets
5
Net score
5
Total upvotes
Showing
721–750
of
770
public snippets
Java
ReentrantLock — Beyond synchronized
`ReentrantLock` does what `synchronized` does, plus: tryLock with timeout, fair queueing, interruptible acquire, multiple condition variables. Use when synchronized's rigidity bites.
Jun 18, 2026
0
Rust
Cargo Workspace Setup
A workspace lets a single repo contain multiple related crates (e.g. core lib + CLI + server) that share `target/` and dependencies. The root `Cargo.toml` lists members.
Jun 18, 2026
0
Python
Protocol — Structural (Duck) Typing
Define what an object can DO, not what it IS. A class doesn't need to inherit from a Protocol — it just needs to match the shape. Like TypeScript interfaces with `implements` optional.
Jun 18, 2026
0
PHP
Markdown to Plain Text
Strip common Markdown syntax to produce a plaintext preview suitable for excerpts, search indexes, or email subjects. Handles headings, bold/italic, links, images, code blocks, and lists.
Jun 18, 2026
0
Bash
Squash Last N Commits
Combine the last N commits into one before merging a feature branch. Avoids hours of "WIP" / "fix typo" commits cluttering history.
Jun 18, 2026
0
SQL
IS DISTINCT FROM — Null-Safe Equality
`=` returns NULL when either side is NULL. `IS DISTINCT FROM` (and its inverse `IS NOT DISTINCT FROM`) treat NULLs as equal to themselves — the right tool for change detection.
Jun 18, 2026
0
PHP
Rate Limiter (token bucket, file-backed)
A dirt-simple rate limiter that throttles per-key using a token bucket persisted to a JSON file. Good for single-server protection of expensive endpoints; reach for Redis when you scale out.
Jun 18, 2026
0
Bash
Parallel Map with xargs -P
`xargs -P N` is a Swiss-army knife for parallel map: feed it a list of inputs and a command, it runs N workers. Faster and simpler than bash for-loops with `&`.
Jun 18, 2026
0
Rust
if let / while let
When you only care about one variant, `if let` is shorter than `match`. `while let` keeps looping as long as the pattern matches — great for draining iterators or option-returning APIs.
Jun 17, 2026
0
Bash
Pad String to Fixed Width
Pad strings left or right so columnar output lines up. printf gets you about 95% of what you need; the helpers wrap it for readability.
Jun 17, 2026
0
Bash
Uppercase / Lowercase / Title Case
Bash 4+ has built-in case conversion via parameter expansion. No need for `tr` or `awk` for most cases.
Jun 17, 2026
0
JavaScript
Truncate String
Truncates a string to a maximum character length and appends an ellipsis (or custom suffix). The smart version breaks at the last word boundary to avoid cutting in the middle of a word, making truncated text look natural in card previews, notifications, and list views.
Jun 17, 2026
0
SQL
Gap Detection — Missing Days / Sequences
Find holes in a series — missing invoice numbers, days with no events, gaps in a sequence. Combine `LAG` (or `generate_series`) with a join to spot them.
Jun 17, 2026
0
Bash
Reset Branch to Match Remote
Throw away local commits and resync to origin. Destructive — make sure no local work is at risk first.
Jun 17, 2026
0
SQL
UUID Primary Keys
UUIDs as PKs avoid sequence contention across services and let clients generate IDs offline. v4 is random (worst for B-tree indexes); v7 is time-ordered (B-tree friendly).
Jun 17, 2026
0
JavaScript
Create Element Helper
A concise helper for creating DOM elements with attributes, properties, styles, event listeners, and children in one call — similar to hyperscript or JSX without a build step. Eliminates repetitive createElement / setAttribute / appendChild chains and keeps DOM construction code readable.
Jun 17, 2026
0
Go
defer / panic / recover
`defer` runs a statement when the enclosing function returns — LIFO order. `panic` aborts; `recover` (inside a deferred func) catches a panic and converts it back to a normal return. Use sparingly.
Jun 17, 2026
0
SQL
FIRST_VALUE / LAST_VALUE
Return the first or last value within a window. Useful for "compare each row to the partition's first/last value" — for example, "how much have we grown since the user's first order?"
Jun 17, 2026
0
PHP
Simple File-Backed Cache
A tiny key-value cache with TTL, persisted as a serialized PHP file per key. Good enough for memoizing expensive computations on a single machine; replace with Redis when scaling out.
Jun 17, 2026
0
Rust
clap — Derive-Style CLI Parser
`clap` with the derive macro turns a Rust struct into a fully-featured CLI: `--help`, `--version`, validation, subcommands. The most popular CLI framework in the Rust ecosystem.
Jun 17, 2026
0
JavaScript
Calculate Reading Time
Estimates the reading time of a block of text based on an average adult reading speed (default 200 words per minute). Strips HTML tags before counting to handle rich-text content. Returns the result in minutes, rounded up, so a short article always shows at least "1 min read".
Jun 17, 2026
0
SQL
LAG / LEAD — Previous and Next Row
Access the row before (`LAG`) or after (`LEAD`) the current one, in the window's order. Perfect for "delta from previous reading", session boundaries, etc.
Jun 17, 2026
0
SQL
EXISTS vs IN — Subquery Filtering
Both check membership. `EXISTS` is usually faster for big subqueries (stops at first hit). `IN` is more readable for small fixed lists. `NOT IN` is dangerous with NULLs — prefer `NOT EXISTS`.
Jun 17, 2026
0
JavaScript
Base64 Encode & Decode (Unicode-safe)
Encodes and decodes strings to/from Base64. The native btoa/atob only handles Latin-1 characters, so these wrappers use TextEncoder/Uint8Array to handle the full Unicode range including emoji and CJK characters. Useful for encoding binary data, tokens, and payloads for URLs or localStorage.
Jun 17, 2026
0
PHP
Cron Singleton with flock
Make sure only one copy of a long-running cron job runs at a time. Uses a non-blocking advisory lock on a sentinel file — second invocation exits immediately if the lock is held.
Jun 16, 2026
0
TypeScript
Template Literal Types
Build string types out of other string types. Lets you express patterns like "every CSS event handler starts with on" in the type system itself, with autocomplete to match.
Jun 16, 2026
0
Kotlin
init Block + Constructor Order
`init { }` blocks run during construction, in declaration order, interleaved with property initializers. Use to validate the primary constructor or set up derived state.
Jun 16, 2026
0
JavaScript
Scroll to Element Smoothly
Scrolls the page to a target element with an optional pixel offset (useful when a fixed header is present). Uses the native scrollIntoView with smooth behaviour when supported, and falls back to a manual scrollTo with a configurable offset. Pass a CSS selector string or an Element reference.
Jun 16, 2026
0
Kotlin
Data Classes — Records Done Right
`data class` auto-generates `equals`, `hashCode`, `toString`, `copy`, and `componentN` (destructuring). Replaces boilerplate POJOs / Records / classes-with-lombok in one line.
Jun 16, 2026
0
Bash
Substring + Find/Replace (parameter expansion)
Bash's ${var:offset:length} and ${var//pattern/replacement} let you do most string operations without ever spawning sed. Faster, and the syntax is portable across modern shells.
Jun 16, 2026
0
1
…
23
24
25
26