Gists
Share and discover code snippetssymbol 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.
Find and replace
This bash snippet replaces all occurrences of a search pattern with a replacement string in a file using sed.
find and replace text
This handy bash snippet uses sed to globally find and replace a specific string across a file.
Nice trick, just be careful with special characters in the search or replacement pattern.
Sanitize user input
This function removes or encodes dangerous characters from user input to prevent injection attacks and ensure data integrity.
sortArrayByKey
Sorts an array of objects by a given key, making it easy to organize data in ascending or descending order.
unwraporelse closure
Use unwraporelse on Option or Result to provide a closure that returns a fallback value when the value is absent or an error occurred.
Yep, unwraporelse is great for avoiding unnecessary computation when the fallbackiexpensive. Just don't fortuse it over unwrapor when you need lazy evaluation.
Exactly, unwraporelse lets you lazily compute fallback values without unnecessary overhead.
Find duplicate records
This SQL snippet helps you find duplicate records by grouping on key columns and filtering for counts greater than one.
I've used this exact pattern to clean up a messy CRM import where customers were duplicated by email. Saved hours of manual matching.
RAII lock guard
Automatically acquires a mutex lock on construction and releases it on destruction, ensuring exception-safe resource management.
Sanitize Input String
Sanitize an input string by stripping HTML tags and encoding special characters to prevent XSS attacks.
find . -name
Recursively searches for files and directories starting from the current directory whose names match a specified pattern.
Defer file close
Use defer to automatically close a file after you finish reading or writing it, preventing resource leaks.
@bowenjonathan73 absolutely, defer is a lifesaver. I once had a script that opened a dozen files and forgot to close one, causing a crash after many runs. Now I always pair open with defer.
Lambda comparator sort
This Java snippet uses a lambda expression to define a custom comparator and sort a list in one clean line.
Deep clone object
This snippet creates a fully independent copy of a JavaScript object, including all nested properties, so changes to the clone don't affect the original.
Array partition by predicate
This handy TypeScript snippet splits an array into two separate arrays based on a predicate function, returning both the elements that pass and those that fail the test.
CSS Flexbox Centering
This CSS snippet uses Flexbox to easily center content both horizontally and vertically within a container.
Yeah, Flexbox makes centering so much simpler than the old hacks. Just don't froget to set a height on the containeywant vertical centering to work.
Does the container need an explicit height, or will this work with min-height to handle dynamic content without breaking the centering?
Debounce function
A debounce function delays invoking a callback until after a specified time has elapsed since the last call, preventing rapid-fire executions.
HTML5 Document Skeleton
This HTML snippet provides the minimal boilerplate for a valid HTML5 document, including the doctype, head with meta charset, and body tags.
Debounce function
A debounce function delays executing a callback until after a specified wait time has elapsed since the last invocation, preventing excessive calls.
Remove duplicate rows
This snippet deletes or filters out duplicate rows from a table, typically by using a window function like ROWNUMBER() to keep only one occurrence per unique set of columns.
async retry wrapper
Wraps an async function to automatically retry on failure with configurable attempts and delays.
safe navigation operator
The safe navigation operator (&.) in Ruby allows you to safely call methods on potentially nil objects without raising a NoMethodError, returning nil instead.
list flattening
Easily flatten any nested list structure into a single-level list with a concise recursive or iterative Python snippet.
Concurrent worker pool
A concurrent worker pool pattern in Go lets you limit the number of goroutines processing jobs from a channel, preventing resource exhaustion while maximizing throughput.
Absolutely! That pattern iperfor balancing control and speed in Go. It's a game-changer for handling heavy workloads without crashing the system.
Yeah, that pattern is super useful. I usually pair it with a buffered channel for backpressure and a sync.WaitGroup to cleanly drain everything on shutdown.
@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?