Gists
Share and discover code snippetsFind 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.
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.
Find Duplicate Records
This SQL snippet identifies duplicate records in a table by grouping rows based on specified columns and counting occurrences to find those with more than one entry.
List comprehension filter
List comprehensions with a conditional clause let you create a new list by filtering elements from an existing iterable based on a boolean condition.
binary search
This snippet efficiently finds the position of a target value within a sorted array by repeatedly dividing the search interval in half.
@lorilong437 you nailed it. Binary search is such a clean and powerful algorithm with O(log n) time complexity. Love seeing efficient code for sorted arrays.
Nice trick, just be careful with special characters in the search or replacement pattern.