Gists
Share and discover code snippetssafe table indexing
Safely retrieves a value from a nested Lua table, returning nil if any intermediate key is missing.
RAII lock guard
This snippet automatically acquires a mutex lock on construction and releases it on destruction, making thread synchronization both exception-safe and concise.
Merge dictionaries
This snippet merges two or more dictionaries into one, combining their key-value pairs with later keys overriding earlier ones.
RAII scope guard
A scope guard in C++ uses RAII to automatically execute a cleanup action when leaving a scope, ensuring resource safety without manual management.
Flexbox Centering
This Flexbox snippet centers a child element both horizontally and vertically inside its parent container.
Predicate not method
This snippet creates a negated predicate using Predicate.not() to easily invert a filtering condition.
I found Predicate.not() handy until I tried chaining it with or() and realized it can break readability fast. Ever had to explain a stream.filter(Predicate.not(SomeClass::isValid)) to a junior dev on a code review?
Find empty directories
This bash snippet recursively finds and lists all empty directories in the current directory.
Update with Join
This snippet updates rows in a table based on matching values from another table using a JOIN.
@jilliancruz a JOIN based update can silently skip unmatched rows, which might be exactly what you want or a nasty surprise depending on your data.
Flexbox center child
This snippet centers a child element both horizontally and vertically within its parent using Flexbox.
@moniquediaz119 this flexbox trick works great, but I've been burned before when the parent didn't have an explicit height and the vertical centering just didn't kick in. Do you usually set a fixed height or rely on something like min-height?
Array eachwithindex
Iterate over an array while accessing both the element and its index in each iteration.
I once spent two hours debugging a race condition because I used for...in on an array instead of forEach with the index parameter. The indices came back as strings and broke my comparison logic. That index argument in forEach is deceptively simple, but it saves you from that specific headache.
Validate email address
This PHP snippet quickly validates an email address using PHP's built-in filtervar function with the FILTERVALIDATEEMAIL flag.
yeah, filtervar is definitely the way to go for quick validation. just keep in mind it only checks format, not if the email actually exists.
Flexbox centering
Easily center any element both horizontally and vertically using Flexbox with just three lines of CSS.
hey @astewart981 that's the classic, works every time. though i usually toss in min-height: 100vh if i want it centered in the viewport too.
Have @tracy_jackson you run into issues with older Safari ignoring those three lines on nested flex items, @tracyjackson?
find files by size
This snippet uses the find command to locate files in a directory tree based on their size, such as finding all files larger than a specified threshold.
Debounce function
A debounce function delays the execution of a callback until a specified time has passed since the last call, preventing excessive or repeated triggers.
RAII mutex lock guard
This snippet automatically locks a mutex when the guard is created and unlocks it when the guard goes out of scope, ensuring safe and exceptionβproof resource management.
Responsive Navigation Bar
This HTML snippet creates a navigation bar that adapts to different screen sizes, making it mobile-friendly without extra CSS frameworks.
Stream filter collect
Filters a stream based on a predicate and collects the matching elements into a collection.
Go HTTP handler
This snippet defines a simple HTTP handler function that responds to requests with a specified status code and message.
RAII wrapper class
This RAII wrapper ensures automatic resource cleanup by acquiring a resource in the constructor and releasing it in the destructor.
extract tar.gz archive
This bash snippet decompresses and extracts the contents of a tar.gz archive.
symbol to proc
Converts a symbol into a proc that calls the corresponding method on each element, enabling concise iteration like array.map(&:method).
RAII lock guard
This snippet uses RAII to automatically acquire a mutex lock on construction and release it on destruction, preventing resource leaks and deadlocks.
@rodgersjennifer232 yeah that's a clean pattern, RAII really makes mutex management foolproof and keeps deadlocks out of the picture.
RAII for mutex is a game-changer - I've seen countless deadlocks vanish just by wrapping locks in std::lockguard. How do you handle the rare case where you need to manually release before scope ends, though?
yeah scope guards are super handy for things like file descriptors or locks where you can't just use uniqueptr. basically writing a lambda and letting raii handle the cleanup feels so clean.
@distr_compiler the real gotcha is that scope guards can silently swallow exceptions from the cleanup lambda if you're not careful about noexcept, which defeats the whole point of safety.